- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2019 10:25 AM
Having trouble getting the below script to work. I'm trying to have an attachment be mandatory on the portal when submitting a catalog item request. It will only be required if a variable is 'yes'. I think the issue may be that I am not using cart layout. I do get a java console error when I click submit but the form submits anyhow.
function onSubmit() {
if (g_form.getValue('multiple_user_access_request') == 'yes'){ //Checks whether the variable value is 'yes'
var cat_id = gel('sysparm_item_guid').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_req_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
alert("Attachment is required in order to submit.");
return false;
}
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2019 10:28 AM
Hi there,
Assuming this is for a Catalog Item on the Service Portal:
To perform an onSubmit check if an attachment is attached, you could have an onSubmit Catalog Client Script like:
function onSubmit() {
if(g_form.getValue('multiple_user_access_request') == 'yes') && this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
return false;
}
}
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2019 10:28 AM
Hi there,
Assuming this is for a Catalog Item on the Service Portal:
To perform an onSubmit check if an attachment is attached, you could have an onSubmit Catalog Client Script like:
function onSubmit() {
if(g_form.getValue('multiple_user_access_request') == 'yes') && this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
return false;
}
}
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2019 10:58 AM
This solved the issue! Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2019 10:30 AM
Please take a look at this.
https://www.servicenowelite.com/blog/2017/5/12/service-portal-require-attachments
Please mark my response as correct and helpful if it helped solved your question.
-Thanks