Check Attachments on current form.

ankit025
Kilo Contributor

Is there a way to check for attachments in the current form and making attachment mandatory if a field is checked ?

1 ACCEPTED SOLUTION

Hi Ankit Lohani



Try the below script,If you are looking for mandatory attachments on catalog item.


function onSubmit() {


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


var gr = new GlideRecord("sys_attachment");


gr.addQuery("table_name", "sc_cart_item");


gr.addQuery("table_sys_id", cat_id);


gr.query();


if (!gr.next()) {


alert("You must attach a file to submit.");


return false;


}


}


View solution in original post

13 REPLIES 13

Hi Ankit Lohani



Try the below script,If you are looking for mandatory attachments on catalog item.


function onSubmit() {


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


var gr = new GlideRecord("sys_attachment");


gr.addQuery("table_name", "sc_cart_item");


gr.addQuery("table_sys_id", cat_id);


gr.query();


if (!gr.next()) {


alert("You must attach a file to submit.");


return false;


}


}


Hi Pradeep, this seems to work, but it's showing an alert even though a file has been attached to the form.


I was pointing to the wrong table that seems to work.


Hi Pradeep,



The script you provided helped me make attachments mandatory on my catalog item. Since I am new to ServiceNow, please excuse my ignorance. Just out of curiosity, I wanted to know if we can use the Gliderecord object in our client scripts? I was of the impression that because Gliderecord is a server side object, it cannot be used in client scripts. But then again, the script you provided ran perfectly fine in a client script. Please help to resolve this dilemma.


Hi Kunal,



Client side GlideRecord enables the use of some GlideRecord functionality inside client scripts. However client script GlideRecord has limited set of functionality.


Hence make sure that query are optimized and they don't run synchronously. The alternative approach is client script + GlideAjax. Please refer the below link for more info.


http://wiki.servicenow.com/index.php?title=GlideAjax



I hope this helps