How to check whether an attachment exists before submitting

Koji Yanase
Tera Contributor

I'd like to avoid submitting without any attachment using record producer. And I wrote a catalog client script as below.

But at the point of time, we can't get the sys_id of the submitting record. And it seems that sys_attachment table doesn't have the record yet. So the client script always alerts.

Is there any way to show an error message when submitting with no attachment? The page where users input values needs to stay.

Thanks.

function onSubmit() {

// alert(g_form.getUniqueValue());

var att = new GlideRecord('sys_attachment');

att.addQuery('table_sys_id', g_form.getUniqueValue());

att.query();

if(!att.next()) {

  alert("No attachment");

  return false;

}

return true;

}

1 ACCEPTED SOLUTION

ankitbadola
Giga Expert

You have to create a catalog client script .



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;  


}    


}



its not a best practice....but a work around ...best practice is you have to create a script include and call a glide ajax


View solution in original post

6 REPLIES 6

Hi,

 

I have used to same code you provided. but no luck. There is no action being taken when I am trying submit the form. 

 

I am using it for particular catalog item. any thoughts ?

 

thanks,

VC

Sontel
Tera Contributor

Anyone who's wondering why this code is not working in current time though following all the suggestions, then let me tell you that there is a new option brought by SN from London release called Isolate script. This option is brought by SN to discourage the use of DOM in SN scripting.

For all existing configurations before London (both OOB & custom), Service Now has set the Isolate script to False so it won't impact any of your existing scripts. For all new configurations that were introduced as part of London(or future releases), the Isolate script is set to True.  

So if it is a new script then please set the Isolate script to False and then try this code. By default Isolate script option is not over the form so you have to personalize the form to see that checkbox.