How to get number of attachments for a catalog item in client script on submit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 01:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 01:25 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 01:31 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 01:28 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 01:28 AM
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....