Delete All Attachments Before Submit on Nintex Form

Document created by chaddavis on May 16, 2018
Version 1Show Document
  • View in full screen mode

I came across a situation where I needed to conditionally hide the attachments field. However, if someone had added attachments, and then decided they no longer needed to fill out that section of the form, I wanted to ensure the attachments were not uploaded if they didn't manually remove each one. 

 

In researching online, and even reaching out to Nintex Support, I didn't come across anything that worked seamlessly, regardless of the attachment quantity that had been added. So, I wrote a custom JavaScript function, and wanted to share it in case anyone else came across the same problem.

 

function ClearAllAttachments(){     
     NWF$('td.ms-propertysheet a').each(function(){     
          var str = NWF$(this).attr('href');     
          var parms = str.substring(50,str.length - 1).replace(/"/g,'').split(',');
          NWF.FormFiller.Attachments.RemoveLocal(parms[0],parms[1],parms[2]);     
     });
}

*I do want to state that if anyone has a better way to do this, or they see ways to improve my code, please add your comments below.

 

To use the code, simply call the function from within some other JavaScript, or even from within a rule. I've attached a simple form example that you can import and try out.

 

Here is a basic example showing the function in use:

NWF$(document).ready(function (){     
     NWF$('#' + choices).change(function(){
          ClearAllAttachments();
     });
});
function ClearAllAttachments(){     
     NWF$('td.ms-propertysheet a').each(function(){     
          var str = NWF$(this).attr('href');     
          var parms = str.substring(50,str.length - 1).replace(/"/g,'').split(',');
          NWF.FormFiller.Attachments.RemoveLocal(parms[0],parms[1],parms[2]);     
     });
}

In the example form, there is a drop down with two choices: Yes & No. Whenever that drop down is changed, the ClearAllAttachements() function is run, which removes every item in the attachment control.

 

Explanation of Function Code:

I noticed that the Delete link after each attachment was held in a table cell with the .ms-propertysheet class, and this was the only place it was used. So, the first line of the function looks for all link elements inside of table cells with that class. For each link element found, it then grabs the href attribute text. I tried simply using a .click() at this point, but it wouldn't work. 

 

If you've looked at where those Delete links are pointing, you'll notice they look something like this:

javascript:NWF.FormFiller.Attachments.RemoveLocal("attachRow79adca87-20cb-43b0-be0f-c2ce020527e50","fileupload79adca87-20cb-43b0-be0f-c2ce020527e5","somefilename.pdf")

So, since I couldn't use .click() to run the code, I figured I'd just deconstruct it and recreate it.

 

After the href string is acquired, it strips off everything not inside the parentheses. Next, it removes all of the double quotes with a little regex, and lastly splits the string into an array by comma. This means I can simply call NWF.FormFiller.Attachments.RemoveLocal myself, and pass in each of the three parameters now in the array, which is done in the last line.

 

One last thing to mention is that this was needed specifically for before the save/submit button was clicked. This function will not work if you want to remove attachments from an already submitted item, because the Delete link code is different. It uses NWF.FormFiller.Attachments.RemoveAttachmentFromServer.call instead of NWF.FormFiller.Attachments.RemoveLocal However, the same process should be able to be used to grab the href text. 

1 person found this helpful

Outcomes