Script to Run on catalog item to Count and Limit Attachment Number
This content was last updated in 2022. It may still be useful, but could contain outdated information. Please confirm its accuracy before relying on it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2021 04:58 AM
HI
I need to limit the number of attachments to 1 on the catalog item while ordering
i have checked yours previous answer available here Limit number of attachements on catalog Item - Developer Community - Question - ServiceNow Community but it not working.
i written some code to find attachment is attached or not, but how to add filter to count the number of attachments
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2021 06:32 AM
Hi,
I have written blog on this.
It should work in both native + portal
Please mark blog as helpful and also bookmark
Verify Mandatory Attachments Count on Catalog Item
Adding here for your reference
For example: You need to ensure there are exact 1 attachment added before submission
Script: onSubmit
Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works
Isolate Script: Set to False
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 add ' + countRequired + ' 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 ' + countRequired + ' attachments before submitting this request.');
return false;
}
}
}
Regards
Ankur
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2021 07:26 AM
Thank you
can a particular type of attachments
here in this case i need to add only excel files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2021 07:31 AM
Hi,
If my response helped please mark response as correct and helpful to close the question.
Also mark the blog as helpful and also bookmark it
Regards
Ankur
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2021 07:33 AM
Hi,
yes that is also possible but would require some DOM manipulation
Regards
Ankur