Make attachment mandatory based on a variable from Catalog Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 08:31 PM
Hi everyone! I'm fairly new to ServiceNow. I'm having difficulties in making the attachment mandatory based from the value of a variable in Catalog Item.
What I wanted to achieve is when I select Create a New Service, attachment becomes mandatory.
On the same catalog item, I was able auto populate the fields based on the Service Name selected using script include and GlideAjax. What I wanted to achieve is to make mandatory attachment field on a catalog item to be true when Create a New Service is selected and false when not selected using script include and GlideAjax.
Here's my code using script include and GlideAjax.
Script Include
var getService6 = Class.create();
getService6.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getMandatoryDetails: function() {
var service6 = this.getParameter('sysparm_user');
var gr = new GlideRecord('sc_cat_item');
gr.addQuery('sys_id', service6);
gr.query();
// Query user records
if (gr.next()) {
gr.mandatory_attachment = true;
gr.update();
}
},
type: 'getService6'
});
Catalog Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('getService6'); // Script Include Name
ga.addParam('sysparm_name', 'getMandatoryDetails'); // Specific Function Name within the Script Include
ga.addParam('sysparm_user', g_form.getValue('select_type_of_request')); // Value of the the field
ga.getXML(setUpdate);
function setUpdate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('mandatory_attachment', answer);
}
}
Any insights will be very helpful in understanding script include and GlideAjax. Thank you!
- Labels:
-
Script Debugger
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 09:10 PM
hi Steven Karl
refer the following thread you can make field mandatory using onsubmit client script and script include refer the following thread it similar to your question.
If it help mark helpful or correct.
Thanks and regards
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 09:26 PM
This link may be of help to you - New Rocket has published their code for making attachments mandatory and it takes from the portal options. You may find it easier to either modify their code or examine it to see how they adjust the Portal Options to see if you can programmatically do it via your onChange script:
https://serviceportal.io/downloads/require-attachments/
It would likely be easier to have an information box or something displayed when the type of request requires an attachment and use an onSubmit to detect if there was one (and show an error / abort if needed). I know this one isn't a perfect fit (you want the mandatory to be dynamic) but I think it's at least a fall back if for some reason the above widget does not help you with the code you need.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2020 01:06 AM
Hi Steven,
Please refer the below link it may help you:
Regards,
Subhojit Das