How to get number of attachments for a catalog item in client script on submit

Anandini
Tera Expert
 
5 REPLIES 5

chetanb
Tera Guru

 

Create an object for  sys_attachment table table then add query for tablename sc_cat_item and table sys id 

to retirive attachment from the catalog fire query() method and next to that getRowcount() to get total attachments

 

function onSubmit() {

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sc_cart_item');
gr.addQuery('table_sys_id', g_form.getUniqueValue());
gr.query();
var attachmentCount = gr.getRowCount():

f (attachmentCount > 0) {

g_form.setValue('your_attachment_count_field', attachmentCount);  

  • } else {
    g_form.setValue('your_attachment_count_field', 'No attachments found'); 
    }
    }

 

 

If my answer solves your query marked it as correct and helpful.

 

Regards,

Cb

Service_RNow
Mega Sage

Hi @Anandini 

Follow thread it will be helpful:

https://servicenowtips.tumblr.com/ 

how-to-count-the-number-of-attachments-in-client-script 

Please try to below code

var proof = gel('sysparm_item_guid').value;

var rowcount=0;
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", proof);
attachment.query();
while (attachment.next()) {
rowcount++;
}
//alert("Row "+rowcount);
if(rowcount <= 2)
{
alert("message");
return false; 
}
else
{
return true; }
}

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Anandini ,

 

Can you brief your question more ? this does not explain what your quries are ? were do you need those numbers? on form message on any field etc,, ?


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect