We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Script to Run on catalog item to Count and Limit Attachment Number

satya30
Tera Contributor

HI @Ankur Bawiskar 

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

20 REPLIES 20

Barnali Rakshit
Tera Contributor

Hi 

I have tried below code but it is not working ..

Function onSubmit() {
   //Type appropriate comment here, and begin script below
    var sys_id = g_form.getUniqueValue();
    var gr = new GlideRecord('sys_attachment');
    
    //gr.addQuery("table_sys_id", cat_id);
    gr.addQuery('table_sys_id',sys_id );
    gr.Query();
    
    var totalSize=0;
    while(gr.next()) {
    
        
        totalSize = parseInt(gr.size_bytes);
        totalSize=(totalSize / Math.pow(1024, 2)).toFixed(2) * 1;
    
    
    if(totalSize > 5) {
    alert("Attachment size exceeded");
        
        return false;
    }
    }
    
}