Verify Mandatory Attachments Count on Catalog Item
Problem Statement: Need to check if exact 'n' number of attachments are added or not when user submit a request through service catalog from Portal/Native view.
I know, there are couple of questions regarding making number of attachments mandatory on Native UI and Service Portal; so thought of adding this in blog..
The below script would help achieving the same.
For example: You need to ensure there are exact 2 attachments added before submission
Script: onSubmit
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 = 2;
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
alert('You must add 2 attachments 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 add 2 attachments before submitting this request.');
return false;
}
}
}
Isolate Script: Set to False
Native View:
1) When 0 attachment added; alert shown to user and stop submission
2) When 1 attachment is added; alert shown to user and stop submission
3) When exact 2 attachments are added; alert not shown and allowed submission
4) When more than 2 attachments are added; alert shown to user and stop submission
Portal View:
1) When 0 attachment added; alert shown to user and stop submission
2) When 1 attachment is added; alert shown to user and stop submission
3) When exact 2 attachments are added; alert not shown and allowed submission
4) When more than 2 attachments are added; alert shown to user and stop submission
Thanks for reading the blog and do provide your inputs/suggestions if any.
Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.Thanks,
Ankur Bawiskar
https://www.servicenow.com/community/developer-blog/verify-mandatory-attachments-count-on-catalog-item/ba-p/2268495