- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2020 09:05 AM
Hi All,
I want to make attachment mandatory based on a specific field values selected for a catalog item when the user will submit the form.
When the User will select the check box above and try to submit the form then attachment made will be mandatory.
if not checked there will be no message appear and the form will be submitted without any issue.
Thanks,
Sambit
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2020 06:53 AM
Hi,
Please refer below links, it might help you.
OnChange variable to make Attachment Mandatory Client Script
Mandatory Attachment on Submit based on Catalog Item Variable
Mark it correct and helpful.
Thanks
Bhagyashri Sorte.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2020 09:20 AM
Hello,
Refer this link- https://community.servicenow.com/community?id=community_article&sys_id=0f0c5290dbe7f380d82ffb24399619f5
Thanks,
Sagar Pagar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2020 09:20 AM
Hi Sam,
An onSubmit() client script as below should help.
function onSubmit() {
//Type appropriate comment here, and begin script below
var checkboxis=g_form.getValue('yourcheckboxvariablename');//PAss correctly
if(checkboxis=='true') //execute if checkbox is true
{
try { //Works for native UI/backend
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' )
{
alert("Attachment mandatory");
return false;//abort submission
}
}
catch(e) { //Works for Portal
if ((this.document.getElementsByClassName('get-attachment').length == 1) || (this.document.getElementsByClassName('get-attachment').length == 0)){
alert('Attachment mandatory.');
return false;
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2020 09:40 AM
Hi,
var a = g_form.getValue('for_client_own_use');
if(a=='true')
{
var cat_id = g_form.getValue('sysparm_item_guid');
var att = new GlideRecord("sys_attachment");
att.addQuery('table_name', 'sc_cart_item');
att.addQuery('table_sys_id', cat_id);
att.query();
if (!att.hasNext()) {
g_form.addErrorMessage('Please attach an attachment');
return false;
}
}
Please refer below link also for more help
Hope this helps!
If you have any more questions, please let me know.
If I have answered your question, please mark my response as correct and helpful.
Thanks,
Sriram
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2020 09:52 AM
please create onSubmit Catalog Client Script
Note: Ensure Isolate Script field is set to false for this client script
- this field is not on form
- but from list you can set it to false
- by default it is true
Give valid variable name for checkbox variable
function onSubmit() {
//Type appropriate comment here, and begin script below
if(g_form.getValue('checkboxVariableName') == 'true'){
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length == 0) {
alert('You must add attachment before submitting this request.');
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length == 0){
alert('You must add attachment before submitting this request.');
return false;
}
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader