- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2020 05:46 AM
Hello All,
I am trying to make attachment mandatory in catalog item and service portal. Below script working in catalog item but not working in serviceportal.
We are using Add to cart button in service portal.
function onSubmit() {
//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;
}
}
}
Can some please help??
Thanks
Saranya
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2020 07:13 AM
Refer blog below
Verify Mandatory Attachments Count on Catalog Item
Sharing the steps and script here
Script: onSubmit
UI Type - ALL
Applies to Catalog Item - True
Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works
function onSubmit() {
//Type appropriate comment here, and begin script below
var countRequired = 1;
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
alert('You must attach the completed form before submitting this request.');
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length != countRequired){
alert('You must attach the completed form before submitting this request.');
return false;
}
}
}
Isolate Script: Set to False
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2020 07:44 AM
You missed one thing on your old script. you did not include your ui script inside the service portal theme .
eg:
-
In the Left Navigator Bar, go to Service Portal > Portals
-
Click the Portal you want to adjust. It maybe be the one with URL suffix of "sp".
-
Click the "info" button for the Theme. The standard theme is "Stock" or “La Jolla”
-
Scroll to the bottom to the JS Include Related List
-
Add your JS Include there
Create New JS Theme
Display Name: GlobalCatalogItemFunctions
UI Script: GlobalCatalogItemFunctions
This is the reason your are getting error in console. at least give a try to so you will be sure what was you missing.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2020 07:49 AM
you should at least try with my last response which you were missing. I tested that in my personal instance and working after that. include your ui script in portal theme then that method would be reachable on portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2020 01:23 AM
Hello Harshan,
Thank you very much for help and doing it from your end.I have added the theme and it got worked.
Appreciating your help!!
Thanks a ton!!!
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2020 07:13 AM
Refer blog below
Verify Mandatory Attachments Count on Catalog Item
Sharing the steps and script here
Script: onSubmit
UI Type - ALL
Applies to Catalog Item - True
Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works
function onSubmit() {
//Type appropriate comment here, and begin script below
var countRequired = 1;
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
alert('You must attach the completed form before submitting this request.');
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length != countRequired){
alert('You must attach the completed form before submitting this request.');
return false;
}
}
}
Isolate Script: Set to False
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2020 07:27 AM
It worked.Thank you very much