Check attachment count using on submit client script

RudhraKAM
Tera Guru

I have a requirement where i need to check if the catalog item have 2 attachments , if not it should not submit , 

i am able to check the attachment by using on submit client script on the catalog item but how and where to include the count ?

Can some one help me with that 

 

 

 

 

function onSubmit() {
//Type appropriate comment here, and begin script below

var proof = gel('sysparm_item_guid').value;
var att = new GlideRecord("sys_attachment");
att.addQuery("table_name", "sc_cart_item");
att.addQuery("table_sys_id", proof );
att.query(att);
if (!att.next()) {
alert("Please include the attachments");
return false;
}
return true;
}

1 ACCEPTED SOLUTION

Shashikant Yada
Tera Guru

You can try with this 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; }
}

https://community.servicenow.com/community?id=community_question&sys_id=cd49c325db5cdbc01dcaf3231f961992

Thanks
Shashikant

Hit Helpful or Correct on the impact of response.

View solution in original post

10 REPLIES 10

Mike Patel
Tera Sage

try below, ref - http://www.servicenowelite.com/blog/2017/5/12/service-portal-require-attachments

function onSubmit() {
	var count = getSCAttachmentCount();
	if(count <= 2) {
		alert('Please include the attachments');
		return false;
	}
}

even though i add 2 attachments its showing the alert 

than change below

count < 2

or 

if(count > 2) {
		alert('Please include the attachments');
		return true;
	}else{

alert('Please include the attachments');
		return false;
}