Check for specific attachment in client script

Marcel H_
Tera Guru

I'm working on a custom project, using custom tables, and one of the requirements is to check for, and alert a user if a specific attachment is missing based on the values of specific fields on the form. This is not for the Service Catalog, but rather specific records on the table.

I currently have a business rule that is set up as shown below. This is working as expected with no issues.

find_real_file.png

find_real_file.png

find_real_file.png

BR Script:

if (current.operation() == "update") {
checkUpdate();

}

function checkUpdate(){
if (!gs.nil(current.sys_id))
{

var grAttach = new GlideRecord('sys_attachment');      
grAttach.addQuery('table_name', current.getTableName());      
grAttach.addQuery('table_sys_id', current.sys_id);  
grAttach.addQuery("file_name",'CONTAINS','VAT');  
 
grAttach.query();      
if (!grAttach.next()) {      
  gs.addErrorMessage('Please attach the associated VAT form.');     // Message to be displayed to user

}  
}}

However, while the BR seems to work well when updating, I'd really like to set this up like an onLoad Client Script, where the message would appear when the form loads so that the person working with the record will be notified of missing attachments regardless of whether they update it or not.

I'm sure perhaps there is a way to do that with a BR, but the other settings I've tried don't seem to work, and not sure why. I've also read some posts here that seem to indicate that a Client Script with a Script Include would be a more efficient way of executing this and doing a query of the attachment table. I don't have much experience working with Script Includes and calling them, and looking at examples already present in the system haven't helped much.

Any assistance would be greatly appreciated.

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Marcel,



Please refer to the solution from Patrick in below post. As you mentioned its not for catalog item, the only change would be to replace line var cat_id = gel('sysparm_item_guid').value; with



var sysid_gel = gel('sys_uniqueValue');


var cat_id = sysid_gel.value;



This will be in addition to the other adjustment like removing lines no 47,48 etc.



Mandatory check for attachment on a form through UI Action


View solution in original post

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Marcel,



Please refer to the solution from Patrick in below post. As you mentioned its not for catalog item, the only change would be to replace line var cat_id = gel('sysparm_item_guid').value; with



var sysid_gel = gel('sys_uniqueValue');


var cat_id = sysid_gel.value;



This will be in addition to the other adjustment like removing lines no 47,48 etc.



Mandatory check for attachment on a form through UI Action


Thanks for the help! The scripts in the link, along with your suggested modifications worked great, and then adding in my requirement for checking that an attachment contains a specific word in the file name was easy enough and worked just like I needed.



One other question if you don't mind, if I am checking for several specific attachments, would it be better to wrap that all into a single Client Script and single Script Include, or separate ones for each attachment that I want to do this for?


You are very welcome Marcel. You can wrap all the logic in single script include with OR conditions depending on your requriements.


dhananjay21
Giga Guru

Try using this one You can do this through client script




var attachments = document.getElementById('header_attachment_list_label');


if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' )


{


alert('You must attach at least 1 file before submitting this request.');


}