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
02-23-2022 08:52 PM
You mean the file should be less than 100kb?
If yes we have a field called Size bytes in attachment field
So you can add one more addquery and check the file size
Sample logic:
gr.addQuery("size_bytes","<","100000"); coverting kb into byte
and for accepting only png or jpg try this:
gr.addQuery("content_type","image/png").addOrCondition("content_type","image/jpeg");
Hope it helps
Thanks
Murthy
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 05:43 AM
That's exactly what I'm looking for. Thank you!
Quick question...I'd like to alert the user if the file is bigger than 100kb and not allow the upload.
Would this work (doesn't seem to alert the user when I test it)?
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.addQuery("size_bytes","<","100000")){
return true;
} else{
alert('File size must be less than 100kb.');
return false;
}
}
}
thank you again!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 10:10 AM
Further to this..
Wondering if it's possible to alert the user before submitting the attachment to the attachment table? It's too bad that the catalog item variable attachment's max_file_size is limited to integers and thus it's lowest value is 1mb.
I'm looking for a way to check file size before submitting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2022 08:15 AM
Wondering why this if statement is not working. Everything is coming back as the "else"
function onSubmit() {
var sys_id = g_form.getUniqueValue();
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id',sys_id);
att.query();
if (att.size_bytes > 100) {
var confirm_box = confirm('Too Big!');
return false;
}
else{
var confirm_box2 = confirm('Size ok');
return true;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2022 09:40 PM
Are you attaching the attachement using attachement variable or using attachement clip icon under catalog?
If you are adding attachment using attachment variable we can try this poosiblity.
Let me know so that i will test in my PDI
Thanks,
Murthy
Murthy