Make an attachment mandatory based on a variable selection on a catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2018 04:23 PM
I'm trying to make an attachment required on a catalog item (we use the portal) which I've done before using Catalog Client script. The one I did that works is very simple because the variable has specific options however, now I need to make attachments mandatory and can't figure out how I can do this. For this catalog item I have a checkbox variable and I want to code it so that if the specific checkbox is selected, an attachment will be mandatory but I don't know what the value of a checked checkbox is.
This is my code:
function onSubmit() {
if (g_form.getValue('tass_merged_update_LUR') == 'true') {
//Works in non-portal ui
try {
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
alert('You must attach the completed form before submitting this request.');
return false;
}
}
//For Service Portal
catch(e) {
var count = getSCAttachmentCount();
if(count <= 0) {
alert('You must attach the completed form before submitting this request.');
return false;
}
}
}
}
Currently, I have the variable value as true because I want it to apply when the checkbox on the variable identified is checked. True does not work. Is it possible to have a catalog client script apply when a checkbox variable is selected? What does the value need to be for the checkbox variable? I have other variables I can use and I was thinking I could use a "is not empty" value but I don't know how to code that.
Suggestions greatly appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 11:29 PM
I did via DOM Manipulation, it worked but may break in future.
But in Madrid, it seems to have a checkbox for mandatory attachments on catalog item. Once Upgraded to Madrid, need to change the piece of code.
WorkAround :
if (this.document.getElementsByClassName('get-attachment').length == 0)
{
alert('Please attach a file before submit');
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2019 01:32 PM
Does this work for the native view? because it doesn't work for me

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2019 11:39 AM
Try replacing the following line in the onSubmit function:
var attachment = document.getElementById('header_attachment_list_label');
with:
var attachment = g_form.getControl('header_attachment_list_label');
It works more consistently in the "ITIL role" view for the "try" conditional to work correctly, and kicks off the "catch" in the Service Portal as expected. The above update works in IE, Edge and Chrome consistently.
This system is currently in use in our Madrid instance, despite the fact there is now the "Hide Attachment" and "Mandatory Attachment" options available under the Portal Settings tab of the Catalog Item configuration page. In the Catalog Item, the attachment requirement is not mandatory unless specific form fields have a specific value. It would be great if there was a way of flagging the attachment requirement as a ".setMandatory()" or via UI Action, but I have had no success with it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2019 12:13 PM
g_form.getControl only works on the platform and not on the Service Portal/Mobile, which would render the script unusable in this use case.
https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_GlideFormGetControl_String

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2020 08:36 AM
You are correct but please review the code again. The JavaScript in the try {} is for the ITIL role browser UI, and the code in catch {} is for Service Portal UI/Ajax. Honestly, I do not like code that uses try/catch to assume an absolute fallback but it works. I have not tested this solution in the legacy mobile app or the Now Agent mobile app, but as the requirement is for uploading a spreadsheet it is unlikely that an iPhone or iPad is going to have the required attachment. If that changes, the Catalog item will be split and we'll lean on the "Attachment Mandatory" checkbox configuration instead of this solution. As the attachment requirement is scoped as a "5% of the time", this solution works for now in three browsers under enterprise support: IE, Edge and Chrome.
Below is an augmented version of what is above:
function onSubmit() {
// clear all previous messages
g_form.clearMessages();
// catalog item form conditional
if (g_form.getValue('catalog_item_form_field') == 'catalog_item_form_field_value') {
// For Non-Portal UI, ITIL role view
try {
// Doesn't work in Filter Navigator view, removing for .getControl instead
// var attachment = document.getElementById('header_attachment_list_label');
var attachment = g_form.getControl('header_attachment_list_label');
if (attachment.style.visibility == 'hidden' || attachment.style.display == 'none') {
var itilmsg = 'You must attached an excel spreadsheet before submitting this request as Bulk. Click the paperclip icon in the top gray bar of the content frame to upload a file.';
g_form.addErrorMessage(itilmsg); // error message at top of form
alert(itilmsg); // browser javascript popup with OK button
return false; // cancel submit
}
}
// For Service Portal
catch(e) {
var count = getSCAttachmentCount(); // Call Script Include to return attachment upload count
if (count <= 0) {
var spmsg = 'You must attached an excel spreadsheet before submitting this request as Bulk. Click the paperclip icon at the bottom of the form to upload a file.';
g_form.addErrorMessage(spmsg); // error message at top of form
alert(spmsg); // browser javaScript popup with OK button
return false; // cancel submit
}
}
}
return true; // allow form submission
}