Is there any way to pre-populate documents in file upload field?

Requirement: Suppose if there are 10 rows in an editable grid of which one column is file upload file. If I upload a document in any of the rows for the first time, the file upload fields of other rows needs to be populated with the same document uploaded. After that whenever a document is updated in any of the rows, the new document should be saved. Kindly suggest a way. TIA

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer
    Yes, it is possible.
    When you are uploading a document for the first time in a file upload field, update a rule input say ri!doc with the document id of the uploaded file. You can do that in the saveInto parameter. Similarly, you will have rule inputs for all other document upload fields say ri!doc1, ri!doc2,etc.
    Now, for all other file upload fields, in the value parameter, check if rule input corresponding to that file upload field is null say isnull(ri!doc1), isnull(ri!doc2), etc.. If it is null, make the value as ri!doc else keep it as ri!doc1....
  • 0
    Certified Lead Developer
    Hi Arun,
    You need to loop the Grid row by using the for each, then in the contents take a fileupload field and in that component value and save into use the same ri variable.
  • If you use the same rule input for every row, when you modify the file (after uploading the first) then it will be the same for the rest.

    What he can do is set a variable to know wether all the files are "null" or not, with that you can know If you have to save into every input the same file or not.
  • Thanks for the solution.
    I tried this way. But this brings a new issue: "Once I upload a document for the first time, it gets auto populated to all other rows. But when I try to update any row with a new document, only the updated document gets saved." I found this error is due to the same document ID existence for all the fields being auto populated.
  • 0
    Certified Lead Developer
    in reply to ARUN RAMANATH

    You have to apply similar conditions in the saveInto parameter of each fileUploadField() except the first one.

    isnull(ri!doc1), isnull(ri!doc2), etc.. If it is null, make the saveInto parameter as ri!doc else keep it as ri!doc1....

  • But what if the first file (ri!doc) is just uploaded to the form which is not yet submitted. which means ri!doc will carry a temporary document ID. In such case, when I delete any of the auto-populated rows, the temporary document id pointing to the document gets deleted. Hence the first document is not getting saved whenever it is updated in any other rows.
  • 0
    Certified Lead Developer
    Hi ARUN,
    As you are using the editanble grid, I assume you are using a list type variable for storing the documents and you store the uploaded doc at the specified index. while saving the document check if the list is empty. If the list is empty iterate the document to the length of list( repeat(length(list),document) ) and save it into the list. This ways you are storing the reference of the doc in all the indexes and if you update at the document at any index only that index will be updated and all other indexes remains like that.

    saveInto:if(
    rule!APN_isEmpty(
    list
    ),
    a!save(
    list,
    repeat(
    length(
    list,
    doc
    )
    )
    ),
    a!save(
    list[fv!index],
    save!value
    )
    )
  • Hi  please find the below code

    Hope this will help you

    load(
      local!employees: {{firstName: "John"},{firstName: "Michael"},{firstName: "Mary"},},
      local!doc: rule!APN_generateRepeatedValueForArrayLength(
        local!employees,
        todocument(
          null
        )
      ),
      a!formLayout(
        contents: {
          a!sectionLayout(
            contents: {
              a!gridLayout(
                totalCount: count(
                  local!employees
                ),
                headerCells: {
                  a!gridLayoutHeaderCell(
                    label: "First Name"
                  )
                },
                columnConfigs: {
                  a!gridLayoutColumnConfig(
                    width: "DISTRIBUTE",
                    weight: 3
                  )
                },
                rows: a!forEach(
                  items: local!employees,
                  expression: a!gridRowLayout(
                    contents: {
                      a!fileUploadField(
                        maxSelections: 1,
                        value: local!doc[fv!index],
                        saveInto: if(
                          rule!APN_isEmpty(
                            local!doc
                          ),
                          a!forEach(
                            items: local!doc,
                            expression: a!save(
                              local!doc[fv!index],
                              save!value
                            )
                          ),
                          local!doc[fv!index]
                        )
                      )
                    }
                  )
                )
              )
            }
          )
        },
        buttons: a!buttonLayout(
          primaryButtons: {
            a!buttonWidget(
              label: "Button",
              saveInto: {
                a!save(
                  ri!doc,
                  local!doc
                )
              },
              style: "NORMAL"
            )
          },
          secondaryButtons: {}
        )
      )
    )

  • Thanks for suggesting this approach. I proceeded with this. But I face an issue when I update a document in any of the rows before submitting the form, the auto populated documents in other rows are not getting saved. This is because while auto populating, temporary document id pointing to the first document we upload remains same for the auto populated documents and so on updating any row , we need to remove the earlier uploaded document and then we need to upload a new document. This results in deletion of temporary document id and thereby all the auto populated documents are not getting saved. Kindly let me know how to fix this.
  • 0
    Certified Lead Developer

    Hi ARUN RAMANATH,

    Hope you have arrived to a solution by now. If not can you please attach the code snippet so as to help us understand the problem clearly. This may help us suggest you better approach.