Script to Run on catalog item to Count and Limit Attachment Number
- 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 08:25 AM
can u please let me know how can i do that?
since there is a requirement to do
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2021 09:32 PM
Hi,
this should work in native; will have to check for portal
Ensure Isolate Script Field is set To False for this client script for DOM to work
function onSubmit() {
//Type appropriate comment here, and begin script below
var arr = [];
if(window != null){
$j("a.content_editable").each(function( index ) {
var val = $j(this).text();
arr.push(val.toString());
});
alert('File Names are'+ arr);
return false;
}
}
Output: it gave me 2 files names then you can iterate over each and check the file name starts with keyword or not
once you get all the file names you can iterate and check if it is allowed or not
Please mark my response as correct and close the thread
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
07-15-2021 07:51 AM
Try this if you want to allow only excel files
function onSubmit() {
var sys_id = g_form.getUniqueValue();
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',sys_id);
gr.query();
if(gr.next()){
if(gr.getValue('file_name').indexOf('.xlsx') != -1 || gr.getValue('file_name').indexOf('.pdf') != -1){
return true;
}
else{
return false;
}
}
Thanks,
Murthy
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2021 08:19 AM
but i also need to limit the number of attachments
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 10:53 AM
Could this be modified to restrict the file attachment size to less than 100kb and only to png or jpg?
I'd like this on a client side script so that before user clicks submit it would check the file attachment for both size and extension.
The attachment variable of max_file_size seems to be restricted to mbs so the lowest is 1mb.
Thank you