You are viewing limited content. For full access, please sign in.

Question

Question

Count files uploaded in modern designer

asked on June 4, 2024

This is not so much a question as an answer to my own issue. We are starting to convert some older forms to the modern designer and I have a couple that need to count the number of uploaded files. One case is the submit button is enabled if they have at least one file uploaded, otherwise they can only click the "Return for Correction" button. 

 

Here it the code I came up with:

/*--------------------------------------------------------------------------------*/
/*                         Start: counting uploaded files                         */
/*--------------------------------------------------------------------------------*/
function countUploadedFiles(theUploadFieldID)
{
  let theFieldObject;
  let theFieldCount = 0;
  
  /* get the field object with the ID */
  theFieldObject = LFForm.findFieldsByFieldId(theUploadFieldID)

  /* loop through all field objects with the ID */
  /* in the case of an upload there will be only one */  
  theFieldObject.forEach(function (currField) {
    /* grab the data's length. */ 
    //console.table (currField);
    theFieldCount = currField.data.length;     
  });  
  
  return theFieldCount;
}; /* END: function countUploadedFiles(theFieldIDNumber) */

const uploadFieldID = 50;
const fileCountFieldID = 51;

/* When file upload changes */
LFForm.onFieldChange(function(){   
  //console.log("Upload Changed " ); 
  let fileCount;
  fileCount = countUploadedFiles(uploadFieldID);
  LFForm.setFieldValues({fieldId: fileCountFieldID}, fileCount);
  //console.log("fileCount: " + fileCount);   
}, {fieldId: uploadFieldID});
/*--------------------------------------------------------------------------------*/
/*                          END: counting uploaded files                          */
/*--------------------------------------------------------------------------------*/

 

I am open to any improvements :) 

1 0

Answer

APPROVED ANSWER
replied on June 7, 2024

Good point, here is the updated code so that if someone else needs/wants it. 

 

/*--------------------------------------------------------------------------------*/
/*                         Start: counting uploaded files                         */
/*--------------------------------------------------------------------------------*/
function countUploadedFiles(theUploadFieldID)
{
  let theFieldObject;
  let theFieldCount = 0;
  
  /* get the field object with the ID */
  theFieldObject = LFForm.findFieldsByFieldId(theUploadFieldID)

  /* loop through all field objects with the ID */
  /* in the case of an upload there will be only one */  
  theFieldObject.forEach(function (currField) {
    /* grab the data's length. */ 
    //console.table (currField);
    theFieldCount = currField.data.length;     
  });  
  
  return theFieldCount;
}; /* END: function countUploadedFiles(theFieldIDNumber) */

const uploadFieldID = 50;
const fileCountFieldID = 51;

/* When file upload changes */
LFForm.onFieldChange(async function(){   
  //console.log("Upload Changed " ); 
  let fileCount;
  fileCount = countUploadedFiles(uploadFieldID);
  await LFForm.setFieldValues({fieldId: fileCountFieldID}, fileCount);
  //console.log("fileCount: " + fileCount);   
}, {fieldId: uploadFieldID});
/*--------------------------------------------------------------------------------*/
/*                          END: counting uploaded files                          */
/*--------------------------------------------------------------------------------*/

 

1 0

Replies

replied on June 6, 2024

Very nice, I've worked with similar before! One thing I would note (doesn't affect your code here but just important when working with the LFForm API) is that setFieldValues is asynchronous meaning if you wanted to synchronously run code after setting the value of the upload count field you would need to await it.

2 0
APPROVED ANSWER
replied on June 7, 2024

Good point, here is the updated code so that if someone else needs/wants it. 

 

/*--------------------------------------------------------------------------------*/
/*                         Start: counting uploaded files                         */
/*--------------------------------------------------------------------------------*/
function countUploadedFiles(theUploadFieldID)
{
  let theFieldObject;
  let theFieldCount = 0;
  
  /* get the field object with the ID */
  theFieldObject = LFForm.findFieldsByFieldId(theUploadFieldID)

  /* loop through all field objects with the ID */
  /* in the case of an upload there will be only one */  
  theFieldObject.forEach(function (currField) {
    /* grab the data's length. */ 
    //console.table (currField);
    theFieldCount = currField.data.length;     
  });  
  
  return theFieldCount;
}; /* END: function countUploadedFiles(theFieldIDNumber) */

const uploadFieldID = 50;
const fileCountFieldID = 51;

/* When file upload changes */
LFForm.onFieldChange(async function(){   
  //console.log("Upload Changed " ); 
  let fileCount;
  fileCount = countUploadedFiles(uploadFieldID);
  await LFForm.setFieldValues({fieldId: fileCountFieldID}, fileCount);
  //console.log("fileCount: " + fileCount);   
}, {fieldId: uploadFieldID});
/*--------------------------------------------------------------------------------*/
/*                          END: counting uploaded files                          */
/*--------------------------------------------------------------------------------*/

 

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.