Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Attachment Mandatory with message if attachment isnt provided

HenryD
Tera Guru

Hello, currently i am trying to do a client script that makes my catalog item have to submit a attachment and its mandatory, also want to add a message if they dont add a mandatory for it to submit. i tried using the variable attachment but i need to do it with the user adding the attachment to the paper clip on top of the catalog item. i have been researching tips and tricks but none seem to work for me, if anyone has done this lately or can point me to the righ direction i appreciate it. thank you in advance 

1 ACCEPTED SOLUTION

Hi,

My apologies for all the back and forth. I was hoping that the above code would work, but apparently not.

I've now tested this myself, because I didn't want to suggest you something else and have it be incorrect (previously, I thought the other code pieces did work).

So this works for me:

function onSubmit() {
    var attachments = document.getElementById('header_attachment_list_label');
    if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
        alert("Please add at least 1 attachment in order to submit request");
        return false;
    }
}

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

11 REPLIES 11

Hello thank you for this, i used this in for desktop onSubmit but i got this error when i tried to checkout:

 

onSubmit script error: TypeError: Cannot read properties of null (reading 'document'):
function () { [native code] }

 

not sure how to go about that

Hello,

Please see my previous reply where I mention the "Isolate script" checkbox.

Did you do that step?

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

hello yes, i just unchecked the box and now i tried again, im getting a newer error:

onSubmit script error: TypeError: Cannot read properties of undefined (reading 'getElementsByClassName'):
function onSubmit() {
  
 if (this.document.getElementsByClassName('get-attachment').length == 0) {
  g_form.addErrorMessage(getMessage('attachment_mandatory'));

  return false;
 } 
   
}

 

do i need to fix the script, thank you for your help btw

Hello,

This should be working fine as is, though, I can change this to an alert just for the sake of testing.

The "Isolate Script" field needs to be false(unchecked).

if(this.document.getElementsByClassName('get-attachment').length == 0) {
alert('You must add at least 1 attachment to submit this request.');
return false;
}

Please share a screenshot of your entire client script submit form, if your issue is still not resolved.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi got this error when i replaced the code: Error MessageonSubmit script error: TypeError: Cannot read properties of undefined (reading 'getElementsByClassName'):
function onSubmit() {
  
 if(this.document.getElementsByClassName('get-attachment').length == 0) {
 alert('You must add at least 1 attachment to submit this request.');
 return false;
 }
}