Mandatory check for attachment on a form through UI Action

brown9394
Tera Expert

Hello experts,

How do I have a mandatory check for attachment via ui action? I want the user to submit an attachment before clicking 'submit'. If no 'attachment' then, alert user to add an attachment.

1 ACCEPTED SOLUTION

You can either do this in the client script or via a business rule.   With a business rule you can have it fire on a certain state change or condition and query for the attachment.   So create a before, advanced business rule and the following example script should work for you:


var attachRec = new GlideRecord("sys_attachment");


attachRec.addQuery("table_name", current.getTableName());


attachRec.addQuery("table_sys_id", current.sys_id);


attachRec.query();


if (!attachRec.next()) {


      gs.addErrorMessage("You must attach a complete import template before submitting.");


      current.setAbortAction(true);


}


View solution in original post

8 REPLIES 8

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Here is an example onSubmit client script I have used to verify attachment:


function onSubmit() {


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


      var attachRec = new GlideRecord("sys_attachment");


      attachRec.addQuery("table_name", "TABLE-NAME-HERE");


      attachRec.addQuery("table_sys_id", rec_id);


      attachRec.query();


      if (!attachRec.next()) {


              alert("You must attach a complete import template before submitting.");


              return false;


      }


}


thanks for the reply ..



Is this possible to do via UI Action and not Client Script? I was hoping the functionality can be contained within the UI Action button, so it can be used at a different state, rather than onSubmit.


You can either do this in the client script or via a business rule.   With a business rule you can have it fire on a certain state change or condition and query for the attachment.   So create a before, advanced business rule and the following example script should work for you:


var attachRec = new GlideRecord("sys_attachment");


attachRec.addQuery("table_name", current.getTableName());


attachRec.addQuery("table_sys_id", current.sys_id);


attachRec.query();


if (!attachRec.next()) {


      gs.addErrorMessage("You must attach a complete import template before submitting.");


      current.setAbortAction(true);


}


I tried doing a business rule, it does not seem to work correctly. I did 'before' when 'state' is 'draft' - nothing happened.