
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2019 08:20 AM
Hi all,
I have been looking to write an OnChange client script to make an attachment mandatory when a specific variable has changed. I don't want to use the onSubmit function.
I also don't want to enable 'Add Attachment' mandatory on the catalog item itself, only when a variable has been changed.
For example: When 'Supplier Updates' is selected, a new mandatory selectbox appears, if 'Address' is then selected from the next selectbox, another mandatory selectbox appears, if Remit is selected, this is where an attachment becomes mandatory.
I am not sure how to enable this in the onChange client script. Does anyone have any crafty ideas to make this dream a reality? Thanks!!
Solved! Go to Solution.
- Labels:
-
Service Portal Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2019 10:08 AM
Now corrected!
//catalog client script to check for attachement
function onSubmit() {
if((g_form.getValue('<variable name>') == 'yes') && this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
return false;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2019 10:40 AM
You are amazing, thanks Ian! This works very well, it's not an onChange script, but does not allow the user to proceed unless an attachment is provided, so really is doing the same thing in a different way. Much appreciated!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2020 09:04 AM
This was very helpful! I appreciate it. All I needed was to override the standard ServiceNow error message for mandatory attachments and was able to do this with a simplified variation of your script.
function onSubmit() {
if (this.document.getElementsByClassName('get-attachment').length == 0) {
alert("This request cannot be submitted without adding an attachment. Please use the paperclip to attach, and resubmit.");
return false;
}
}
Cheers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2021 12:51 PM
Help..I am in the same boat and trying to get your script to work in our environment and hitting a roadblock
We have the following
Access Card request form. If the user selects New Card Request, we want the Attachment to be mandatory
Here are the Variables I am working with
Select Box - Type of Card Requested - Variable - type_of_card_requested
Drop down options
For the script you posted I tried to do the following, but it just submitted like normal. I am sure I am missing some formatting.
//catalog client script to check for attachement
function onSubmit() {
if(g_form.getValue('<type_of_card_requested>') == 'New Card Request') && this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
return false;
}
}
Any ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2019 09:40 AM
Hello Cory:
you can check out my article on it:
Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI
If you are in or after madrid release then:
Option to make the attachment mandatory for catalog items.
Hide Attachments/Mandatory Attachments
Please mark as Correct Answer and Helpful, if applicable.
Thanks!
Abhishek Gardade
Hexaware Technologies
Abhishek Gardade

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2019 09:51 AM
Thank you for the response Abhishek. I do not want attachments mandatory for the entire catalog item, rather only 'if' the variable is selected.