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

I modified the code a little, and used it in the UI action. Works as expected. Thanks for your help!


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi James,



Please refer below link and adjust it as per your req.


Mandatory Attachment Before State Change


Hi Pradeep,



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. the workflow is also driven through UI Action, so current.state = 5 needs to go to 10.


Patrick DeCarl1
ServiceNow Employee
ServiceNow Employee

Hi James,



I would follow below code I have used before. The two options above are using gliderecord via client which is not best practice. Below code is using client script to run ajax call to server side to see if catalog item the user is submitting has an attachment.



Is this check for Catalog item or just a form record?


If check is for a form record then just update the query in line 14-15 to search for table vs cat item.



/* create script include with the following info:


Name   == AJAXcheckAttachment


Client Calliable = true


Description == ajax class to be called from catalog item client side to check and see if an attachment has been added to item before user submits order.


Code below


*/




var AJAXcheckAttachment = Class.create();


AJAXcheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  checkAttachment: function() {



  var cat_id = this.getParameter('cat_id');


  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()) {


  return false;


  }


  },



  type: 'AJAXcheckAttachment'


});






/* Client side code with the following info:


Name == checkAttachment


Type == onSubmit


code below


*/




function onSubmit() {


  //Type appropriate comment here, and begin script below



  if(g_form.getValue('floor_tiles') == 'Yes'){   //<-- Custom checker for customer needs Change for new


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


  var ga = new GlideAjax('AJAXcheckAttachment');


  ga.addParam('sysparm_name','checkAttachment');


  ga.addParam('cat_id', cat_id);


  ga.getXMLWait();


  var check = ga.getAnswer();


  if (check == 'false'){


  g_form.hideFieldMsg('floor_tiles_attachment_info');


  g_form.showFieldMsg('floor_tiles_attachment_info','A Cut Sheet will need to be attached to submit this order.','error');


    return false;


  }


  }


}