Expression Error when removing a document from the file upload component.

I have a form that is used to upload documents.  The form works as intended except when removing a document the user just uploads.  Typically, a user can hover over the document icon and it will turn into a "X".  The user can click on that and remove the document so that another document can be uploaded in its place.  When the user does this, the following error is thrown.

Expression evaluation error at function a!fileUploadField: An error occurred while executing a save: java.lang.ArrayIndexOutOfBoundsException: Invalid index (1) for list: valid range is empty.

The code for the fileuploadfield is:

a!fileUploadField(
              label: "File Upload",
              labelPosition: "ABOVE",
              instructions: "Use this to upload speaker documents.",
              target: tofolder(ri!speakerRecord['recordType!PMSO Speaker.fields.speakerFolderId']),
              maxSelections: 1,
              value: local!file,
              saveInto: {
                a!save(local!file,save!value),
                a!save(local!spkrDocRecord['recordType!PMSO Speaker Document.fields.speakerDocumentNo'], local!file[1])
              },
              showWhen: a!isNotNullOrEmpty(local!spkrDocRecord['recordType!PMSO Speaker Document.fields.speakerDocumentTypeId']),
              validations: {}
            ),

  Discussion posts and replies are publicly visible

Parents
  • I solved my own problem. I modified the code to check to see if the local!file is null or empty.  The code now looks like this.

    a!fileUploadField(
                  label: "File Upload",
                  labelPosition: "ABOVE",
                  instructions: "Use this to upload speaker documents.",
                  target: tofolder(ri!speakerRecord['recordType!PMSO Speaker.fields.speakerFolderId']),
                  maxSelections: 1,
                  value: local!file,
                  saveInto: {a!save(local!file,save!value),
                    if(a!isNotNullOrEmpty(local!file),
                    a!save(local!spkrDocRecord['recordType!PMSO Speaker Document.fields.speakerDocumentNo'], local!file[1]),
                    ""
                    )
                    },
                  showWhen: a!isNotNullOrEmpty(local!spkrDocRecord['recordType!PMSO Speaker Document.fields.speakerDocumentTypeId']),
                  validations: {}

    Does anyone else have a better solution?  This seems to work.

  • 0
    Certified Lead Developer
    in reply to Chris.Gillespie

    Manually indexing ("[1]") into an array like "local!file" works fine unless it's empty (as you discovered).  As stefan pointed out previously, the index() function can safely attempt to retrieve the stated index of the array and, when invalid, return a default value (null()) safely without failing.

Reply Children
No Data