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

Atul Kumar2
Giga Guru

Hi Koji,



Event is fired when you upload the file to the record so you can check the event if it is equal to the below.



attachment.uploaded: An attachment has been uploaded. If multiple attachments are uploaded to a record at one time, only one event will be created.



Then you can popup the window for information if attachment is there or not.



Find further information below.


Administering Attachments - ServiceNow Wiki http://wiki.servicenow.com/index.php?title=Administering_Attachments



Regards,


Atul Kumar


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


And if you find this correct solution please mark it helpful and correct as well


Hi Ankit,


Thank you for your reply. Actually table_name is another and not needed. But it works!


Could you tell me why it's not best practice? I tryed to create a script include but it didn't return value well.


Thanks,


Koji