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

Question

Question

How can I determine the number of attachments in my Forms Upload table?

asked on March 4, 2021 Show version history

What I'm trying to do seems like a simple concept, but I've been having a terrible time of it.  I just want to capture how many documents were uploaded on my Form.  To clarify, I have a single File Upload field, which allows multiple uploads, and I want to know how many the user uploaded.

 

I've been trying variations of this script:

  $('.Submit').click(function() {
    var fileCount = $('.files .file').length*1;
  	$('.fileCount input').val(fileCount).change();
  });

The problem I'm having with that is what event I need to tie it to.  Attaching it to the Submit button (so that it calculates when the person is done with the form) causes it to not get picked up in time to carry through to my workflow (which is the next step in my process).  I have not been able to get a reliable trigger for "when upload table changes" to use that.

 

Is there an easier way, or does anyone have a suggestion for how I might get this data?

0 0

Answer

SELECTED ANSWER
replied on March 4, 2021 Show version history

I have a ton of forms that populate fields upon form submission, and it's pretty rare for them to fail to complete that task.

I'm wondering if the code you posted is your entire script...

If it is, then you need to wrap it in a document ready function.  Like this:

$(document).ready(function () {  
  $('.Submit').click(function() {
    var fileCount = $('.files .file').length*1;
    $('.fileCount input').val(fileCount).change();
  }); 
});

 

Without that document ready code, the system is trying to add an event listener to an object that does not exist yet.  When you include the document ready code, then it waits until the form is loaded before running the code inside, so the submit button already exists by the time the code is run to assign it that event listener.

I did a quick test form with both the code exactly as you posted it and the code as I posted it.

With the first way, the fileCount field never changes that I can visually see as I test the form, but with the second way, I can see the fileCount field populate with a value just before the test form submits and reloads.

1 0
replied on March 4, 2021

Thanks Matthew,

I probably should have clarified, the snippet I included in my question was only part of the whole; it was included inside a document ready function.  The frustrating part was that I could also see the field populate if I paused in the submission; even just adding an alert so I had time to look at the field. 

 

However, when I tried to retrieve that value from the Workflow I called next, it came back with nothing.  I did verify that I was retrieving the correct field with an up-to-date Retrieve activity.  I made an assumption that this failure to perpetuate the value came from the last-minute nature of the change, but maybe something else is going on.

 

I'm going to try your function from your other answer to see if I can get it working that way.  Thanks for the help!

1 0
replied on March 4, 2021

Apologies for my assumption about not including the document ready.

I don't think it should matter, but the code I use to trigger things from the Submit function is:

$(".action-btn.Approve").on('click', function() {
  //code goes here
});

I really don't think that should be functionality different than this: 

$('.Submit').click(function() {
  //code goes here
});

But I guess it is possible that the two process slightly differently.

In my case, it's auto-populating some values into a custom notes field, after doing a parsley validation to check for errors, and occassionally a few other things - and it works pretty well, so I'm not really certain why it wouldn't be working for you. sad

Hopefully the other one will work for you, since it's making multiple attempts to update that field.

0 0
replied on March 4, 2021

I've been changing my activities, replacing fields, etc., so I'm not sure what did it but it is working now.  The value we're capturing on submit is now coming into Workflow.  If anyone else is looking to accomplish this same task, the code that Matthew posted here does the job!

2 0
replied on March 5, 2021

I'm happy to hear you got it working. 

0 0

Replies

replied on March 4, 2021 Show version history

Here's a broader approach to doing this.  It updates your counter upon submit, but also upon adding and deleting files.

$(document).ready(function () {  
  
  //run the count update when the form is submitted.
  $('.Submit').click(updateFileCount);
  
  //run the count update when new attachments are added.
  $('.files').change(function(){
    updateFileCount();
    
    //run the count update 3 seconds after attachments are deleted
    //this code is inside the change code block, so that it is reapplied to
    //new rows of data when they are added - otherwise it would only apply to data
    //already in place when the form loaded.
    //it waits 3 seconds because otherwise it will run before the deletion is complete.
    $('.file-del').click(function() {
      setTimeout(updateFileCount, 3000);
    });
    
  }); 
  
  //this function is called from 3 places above, it gets the 
  //count of files uploaded and updates the counter field.
  function updateFileCount()
  {
    var fileCount = $('.files .file').length*1;
	$('.fileCount input').val(fileCount).change();
  }
});

 

1 0
replied on March 4, 2021 Show version history

Well, I was hoping that I had just missed something, but I actually did try the 'files change' approach in an earlier iteration.  I tried again with the code exactly as you have it here, but the change function never triggers when I add a document.  I tried just doing an alert inside that function, then tried uploading and deleting files, but the alert never came.  I have tried in Chrome and Edge.

 

Not sure on the next step, except to keep trying to get the Submit button approach to register.  Sometimes Workflow is just weird about retrieving data; I once spent a couple of hours trying to retrieve a Department value that Workflow just would not see for some reason, despite being able to see on the Form that it was filled.  Will reply again if I make any progress.

1 0
replied on March 4, 2021

Sean, you're probably running into a Forms security feature, which is that if a field is read-only, it won't store the value if set by JavaScript (I really wish they would make this a configurable option). You'd be able to tell by looking at the variables in the completed process, to see if it's blank there, too.

However, if you make the field editable and then use a Field Rule to hide the field and save the value, it should save for you.

 

1 0
replied on March 4, 2021

I've run into that issue before, it's a frustrating one.  Unfortunately, that's not it either, as this field isn't set to read-only.

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

Sign in to reply to this post.