How to make attachment mandatory using on change client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 04:36 AM
Hi Experts
I have the catalog item, I want to make the attachment mandatory with condition of select box, If I select YES option. If select No attachment should not be mandatory Please help me on this.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 09:37 PM
Hi @dhineshkumar ,
You can use attachment variable instead!!
Please refer to this thread : https://www.servicenow.com/community/developer-forum/can-i-make-add-attachment-mandatory-based-on-va....

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2023 08:10 AM
Hi @dhineshkumar ,
Try as below in onSubmit client script. In our project we used same code and made the attachment mandatory.
Unfortunately I don't think it can be done on OnChange Client script, but if you wish you can give a try but the below code works for onSubmit.
function onSubmit() {
if(g_form.getValue('u_selectBox') == 'Yes') {
try {
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
alert('Attachment please.');
return false;
}
}catch(e) {
var count = getSCAttachmentCount();
if(count <= 0) {
alert('attachment please');
return false;
} }
}}
Please mark correct if my response has solved your query.
Cheers,
Mohammed Basheer Ahmed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2023 07:01 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 03:30 AM
Hello Dhinesh,
You can use the following script in onSubmit catalog client script:
function onSubmit() {
var value= g_form.getValue('attachment_mandatory'); ///pass the name of select box (variable name)
//g_form.addInfoMessage(value);
if(value=="Yes")
{
var cat_id = g_form.getValue('sysparm_item_guid');
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 add an attachment before submitting this request.");
return false;
}
}
}
(You can fetch these values from the script include as well)
Image:
If it is useful then, please mark it as Helpful.