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
11-14-2018 05:28 PM
The value of a checked check is 'true'. Your code looks exactly right. I can only image that your Variable name could be incorrect. Or possibly that the condition IS evaluating to true, but the code inside your if block is not executing.
Check your variable name.
Then add a console.log() statement inside your if block to make sure it is executing.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2019 12:22 PM
This script requires a UI Script and JS theme to be created alongside the catalog client script, have you ensured you have these two components?
https://www.servicenowelite.com/blog/2017/5/12/service-portal-require-attachments
If that isn't the issue then:
Move the g_form.getValue out of the if statement so you can re-use the variable in the //For Service Portal section below as well as the non-portal ui section:
Here's an example:
function onSubmit() {
var option = g_form.getValue('var_badge');
try { //Works in non-portal ui
if (option == 'true') {
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
alert('You must include the required attachment.');
return false;
}
}
} catch (e) { //For Service Portal
var count = getSCAttachmentCount();
if (count <= 0 && option == 'true') {
alert('You must include the required attachment.');
return false;
}
}
}
Finally:
I find that SN is also picky depending on what part of the platform I'm in (i.e. transform maps) regarding booleans, so you may want to try your code without the ' ' around true.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 10:56 AM
Are you sure your variable is correct within the script? It looks very similar to "task" and you may have mistakenly typed "tass."
Thanks for the script, it actually helped me out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 11:52 AM
There are various things which doesn't work on service portal.
Few things you need to check before writing a catalog client script that run on service portal are:
1.You need to use only mobile API's or function's mentioned in Servcie Portal API's .
2.In service portal only asynchronous functions will work, even for simple Glide Record calls you need to use call back functions.
3.you need to be careful while using on submit script if you have any data validations from server side , As it is asynchronous it does't wait for validations.
4.UI macros and UI Pages can't be accessed in Service portal, Insted you need to use widgets (which can be called as catalog item variables).
5.DOM manipulations will not work.
When coming to trouble shooting, just inspect the webpage in source tab you will get errors , so that you can easily find which method or script is giving trouble .
Also, I cannot see getSCAttachmentCount() defined in your script.