Restrict attachments size on single catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I have a catalog item where we want to restrict the size of all attachments to 12 MB total. I tried using the attachment variable, but it only allows the selection of ONE attachment. I want the ability to add multiple attachments using the built-in "add attachments" on the catalog item itself and have it calculate the size of all of them and restrict it to 12 MB.
I found the following code that I put in an onSubmit script, but it's not working - perhaps I need to change more elements than the var maxAttachmentSizeMB = 12; line?
>>>>>>>>>>>>>>>>>>>>>>>>>
function onSubmit() {
var attachments = this.angular.element("#sc_cat_item").scope().attachments; // Adjust element ID as needed
var submitForm = false;
var maxAttachmentSizeMB = 12; // Set your desired limit in MB
attachments.forEach(function(attachment){
var sizeBytes = parseInt(attachment.size_bytes);
var sizeMB = sizeBytes / (1024 * 1024); // Convert bytes to MB.
var sizeString = String(attachment.size_bytes); // Get the size as a string to include the units
alert(sizeString);
if(sizeMB > maxAttachmentSizeMB){
alert(attachment.file_name + ' is too large. Maximum allowed size is ' + maxAttachmentSizeMB + ' MB.');
submitForm = false;
}
});
return submitForm;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Ankur,
When you say "you can grab the complete text from here" where do you mean?
Thanks,
Monica
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Sorry forgot to add screenshots
1) search anchor tag with this class "get-attachment ng-binding ng-scope"
2) iterate and extract the file name which includes the size
3) do string manipulation and grab the size in either KB, Bytes or MB
4) then add and validate
Example: I added 3 files so it will have 3 anchor tag with that class, you can extract the text within that anchor tag and do the next steps
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
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
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
2 weeks ago
I doubt the script include based approach will work as the attachment could not be grabbed for the cart.
Please use the approach I shared.
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
2 weeks ago
I believe I shared a working solution.
Did you get a chance to check that?
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