Attachment in Catalog Item restriction
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 03:45 AM
Hi all
Had a query on attachment restrictions. Have a catalog item developed and it uses the OOB attachment paper icon.
Need to keep this, but want to restrict the user from submitting more than 3 attachments.
If user submits more than 3 attachments, need to pop up a error message and restrict submission. Tried 'onSubmit' catalog client script, but we cannot do any DOM manipulation. 'this.document.getElementsByClassName.length', is where the DOM manipulation caused. This is the script used:
function onSubmit() {
//Type appropriate comment here, and begin script below
var getAttach = this.document.getElementsByClassName('get-attachment').length;
//alert(getAttach+"length is");
if (getAttach > 3) {
g_form.addErrorMessage("Maximum number of attachment is 3");
return false;
}
}
Any other solution which can be used for this use case. Please provide if any.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 04:00 AM
this line is not DOM -> this.document.getElementsByClassName.length
If it was DOM then it should not work without Isolate Script=False
Your client script with the above line will work even with Isolate Script=True
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 04:04 AM
you will need to approach this requirement slightly different because, as you mentioned, DOM manipulation is not allowed in catalog client scripts.
use following client script:
function onSubmit() {
var attachmentCount = g_form.getAttachments().length;
if (attachmentCount > 3) {
g_form.addErrorMessage("You cannot submit more than 3 attachments.");
return false;
}
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 10:16 PM
Hi @Community Alums
Still the above code is not VA compatible. 'g_form.getAttachments().length' is where it is showing not VA compatible.
I had previously tried this, but VA compatible is to be taken care of and need to write the script in that way.
Had an idea of writing a function in Script Includes and calling it in the catalog client script, but since the form submission won't wait for Script include to complete the run and thus allowing the form submission to happen.
Please help if any other work around can be used.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 10:27 PM
I believe you are safe to use this line -> this.document.getElementsByClassName.length with Isolate Script = True
There are lots of OOB scripts from ServiceNow which uses DOM manipulation although they say it's not recommended.
Please inform your customer about this.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader