Is it possible to require a user to add an attachment before submitting a catalog item?

Jim Coyne
Kilo Patron

This post actually started out as a question, but as I was typing, I figured out what to do, so now I'm sharing... 🙂

We have an item that requires an attachment in order to fulfill the request, but I was not sure if it was possible to enforce. We could ask the user to confirm there is an attachment, but I could not see how to actually check it with code? Attachments are added to the sys_attachment table and associated with the sc_cart_item table, but there is no guarantee that the attachment is for that particular item, as the record stays around if the item is not actually added to the cart or ordered.

Then that "AHA!" moment struck - just check to see if the list of attachments is visible:

AttachmentList.jpg

Here's the onSubmit script:

function onSubmit() {
  var attachments = document.getElementById('header_attachment_list_label');
  if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
      alert('You must attach at least 1 file before submitting this request.');
      return false;
  }
}
1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Just setting this answer as correct as the actual thread was not meant as a question and it cannot be changed because it came from the original Community site.


View solution in original post

16 REPLIES 16

Jay_Ford
Kilo Guru

I have also implemented this in the past and took a slightly different path by actually querying the sys_attachment table to see if an attachment exists for the current cart.




function onSubmit() {
var cat_id = gel('sysparm_item_guid').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
alert("You must attach the form before proceeding.");
return false;
}
}


Aha, I was missing the


var cat_id = gel('sysparm_item_guid').value;
part. That's what I could not figure out to make sure the attachment was for the current item.

Thanks


+1 for this nice solution. 🙂

And now for the Grand Prize... Is it possible to verify the contents of the attachment as well, to see whether the required document has been attached? One of our clients is thinking of implementing a similar solution, but also needs to check the contents of the attachment, to verify the correct one was added (why this is needed, is an entirely different argument). Any ideas?

Wesley


Would returning the type of file be verification enough?
If so you could use the content_type filed on the sys_attachment table.