How to count the number of attachments in client script?

DINESH CHINNAD1
Giga Expert

Hi Team,

I am working with one requirement, Which is asking to check the attachment before submitting a "change request" by using record producers.

I have the below two variables,

1. Script information(SI) with choice yes/no

2. Backout script information(BSI) with choice yes/no

If SI is yes, I need to make sure the attachment is added, else it won't allow me submit the request

If BSI is yes, I need to make sure the attachment is added, else it won't allow me submit the request

Now i need to make sure two attachments are added in the request before submitting the request, else it won't allow me submit the request.

If either one of them is yes, I am aware that, I can use the below script to check for attachment.

function onSubmit() {

    //Type appropriate comment here, and begin script below

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

var s = g_form.getValue('si');

//alert("Script Info is "+s);

if(s == 'yes')

{

var attachment = new GlideRecord("sys_attachment");

attachment.addQuery("table_name", "change_request");

attachment.addQuery("table_sys_id", cat_id);

attachment.query();

if (!attachment.next()) {

alert("Please attach Script information");

return false;   }

}

}

How to validate my case? If anyone is having an idea please share with me.

Thanks,

Dinesh C

1 ACCEPTED SOLUTION

DINESH CHINNAD1
Giga Expert

Thanks you all for your responses. I have accomplished that by using the below script.



function onSubmit() {


    //Type appropriate comment here, and begin script below


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


var rowcount=0;


var s = g_form.getValue('si');


var bs = g_form.getValue('bsi');


//alert("si "+s);


//alert("bsi "+bs);


if((s == "Attach a file") && (bs == "Attach a file"))


{


//alert("pass in S & BS if");


var attachment = new GlideRecord("sys_attachment");


attachment.addQuery("table_name", "change_request");


attachment.addQuery("table_sys_id", cat_id);


attachment.query();


while (attachment.next()) {


rowcount++;


}


//alert("Row "+rowcount);


if(rowcount == 2)


{


return true;


}


else


{


alert("Please attach Script information");


return false;   }


}


else if(s == "Attach a file")


{


//alert("pass in S if");


var attachment = new GlideRecord("sys_attachment");


attachment.addQuery("table_name", "change_request");


attachment.addQuery("table_sys_id", cat_id);


attachment.query();


while (attachment.next()) {


rowcount++;


}


//alert("Row "+rowcount);


if(rowcount == 1)


{


return true;  


}


else


{


alert("Please attach Script information");


return false;


}


}


else if(bs == "Attach a file")


{


//alert("pass in BS if");


var attachment = new GlideRecord("sys_attachment");


attachment.addQuery("table_name", "change_request");


attachment.addQuery("table_sys_id", cat_id);


attachment.query();


while (attachment.next()) {


rowcount++;


}


//alert("Row "+rowcount);


if(rowcount == 1)


{


return true;  


}


else


{


alert("Please attach Backout script information");


return false;


}


}


}


View solution in original post

6 REPLIES 6

antin_s
ServiceNow Employee
ServiceNow Employee

Hi Dinesh,



You need to use GlideAjax instead of GlideRecord. Using GlideRecord or GlideAggregate in the server side, check if it has the required number of attachments. Following is the sample using GlideRecord and getRowCount()




var attachment = new GlideRecord("sys_attachment");


attachment.addQuery("table_name", "incident");


attachment.addQuery("table_sys_id", '079d0b2fdb70f600d60e78b5ae9619e0');


attachment.query();




gs.log(attachment.getRowCount());



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


sachin_namjoshi
Kilo Patron
Kilo Patron

Hi,



Please use below client script to validate attachments.



function onSubmit() {


    //Type appropriate comment here, and begin script below


    var attach = document.getElementsByClassName('attachment_list_items');


var realattach = 0;


for(var count = 0; count<attach.length;count++){


var child = attach[count].childNodes;



if((child.length) == 1)


{


  realattach += 1;


}


    }


if((realattach-1) == 2)


  return true;


else{


alert('invalid attachments');


g_form.addInfoMessage('Invalid attachmnets');


  return false;


}


}



Regards,


Sachin


DINESH CHINNAD1
Giga Expert

Thanks you all for your responses. I have accomplished that by using the below script.



function onSubmit() {


    //Type appropriate comment here, and begin script below


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


var rowcount=0;


var s = g_form.getValue('si');


var bs = g_form.getValue('bsi');


//alert("si "+s);


//alert("bsi "+bs);


if((s == "Attach a file") && (bs == "Attach a file"))


{


//alert("pass in S & BS if");


var attachment = new GlideRecord("sys_attachment");


attachment.addQuery("table_name", "change_request");


attachment.addQuery("table_sys_id", cat_id);


attachment.query();


while (attachment.next()) {


rowcount++;


}


//alert("Row "+rowcount);


if(rowcount == 2)


{


return true;


}


else


{


alert("Please attach Script information");


return false;   }


}


else if(s == "Attach a file")


{


//alert("pass in S if");


var attachment = new GlideRecord("sys_attachment");


attachment.addQuery("table_name", "change_request");


attachment.addQuery("table_sys_id", cat_id);


attachment.query();


while (attachment.next()) {


rowcount++;


}


//alert("Row "+rowcount);


if(rowcount == 1)


{


return true;  


}


else


{


alert("Please attach Script information");


return false;


}


}


else if(bs == "Attach a file")


{


//alert("pass in BS if");


var attachment = new GlideRecord("sys_attachment");


attachment.addQuery("table_name", "change_request");


attachment.addQuery("table_sys_id", cat_id);


attachment.query();


while (attachment.next()) {


rowcount++;


}


//alert("Row "+rowcount);


if(rowcount == 1)


{


return true;  


}


else


{


alert("Please attach Backout script information");


return false;


}


}


}


Rohan11
Giga Contributor

Have you tried below (to check if 2 attachments exist in onSubmit script)

 

if (parseInt(countAttachments()) < 2){

alert("please put 2 attachments");

return false;

}