The CreatorCon Call for Content is officially open! Get started here.

How to call an AJAX Script Include from a Business Rule

leslie8
Mega Expert

I have a client script that uses AJAX to call a script include to do a validation check. This is working from the Client script. I want to re-use this code to also run the same validations from a Business Rule. How would I structure the call from the Business Rule? How are the parameters supposed to be passed in to the script include from the Business Rule? Or do I need to create a new version of the script include that is designed to take in parameters between parenthesis in the function call block as this example?

  1. var checkStart = new NewDateTimeUtils();  
  2. NewTimeUtils.validateCalendarDate(start_date);  

Client Script

      var start = g_form.getValue('start_date');

      var checkStartFormat = checkDateFormat(start);

                        if (checkStartFormat != 'true') {

                                  g_form.showFieldMsg('start_date', checkStartFormat,'error');

                                  return false;

                                  }

      function checkDateFormat(dateVal) {

              var ajaxCalendarDate = new GlideAjax('ClientDateTimeUtils');

              ajaxCalendarDate.addParam('sysparm_name', 'validateCalendarDate');

              ajaxCalendarDate.addParam('sysparm_userDate', dateVal);

              ajaxCalendarDate.getXMLWait();

  return ajaxCalendarDate.getAnswer();

  }

Script Include

var ClientDateTimeUtils = Class.create();

ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {


validateCalendarDate: function(){

//Client date

var userDate = this.getParameter('sysparm_userDate');

//Check date format will match one of the available system dates

  var ymdDash = /^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/; //yyyy-mm-dd

if (!ymdDash.test(userDate)) {

  return("ERROR: Format must be yyyy-mm-dd");

  }

  else {

  return true;

  }

}
});

4 REPLIES 4

ohhgr
Kilo Sage

Hi Leslie,



Since, business rule already runs on server, you   wouldn't need to call another script include from Business Rule. You can directly use the code inside BR.



However, if it's the re-use of the code that you're aiming to achieve, I'm not sure how to use Client callable Script Include from a BR. May be someone who has done something similar may come up with a strategy.



Thanks,


Mandar


surendarm
Kilo Expert

Hi Leslie,



In an after or before business rule, define the below code.



For example:


function onBefore(curret, previous) //use the same template which servicenow provides by default


{


      var start = current.start_date;


        var checkStartFormat = checkDateFormatV2(start);


    if (checkStartFormat != 'true')


          {  


                        gs.addErrorMessage('start_date error');


                        current.setAbortAction(true);


            }


}


function checkDateFormatV2(dateVal)


{


      var userDate = dateVal


                //Check date format will match one of the available system dates  


                var ymdDash = /^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/; //yyyy-mm-dd  


                  if (!ymdDash.test(userDate))


                  {  


                                    return("ERROR: Format must be yyyy-mm-dd");  


                  }  


              else


              {  


                              return true;


                }


}




You can actually reuse the code but since you have defined with Glideajax definition, for a normal script you cant use the same code.




Instead, you can define the same sets of code written in script include in the same business rule as a different function and call them in   the same business rule.




Try this out and let me know if you have any issues.




Thanks,


Surendar M


coryseering
ServiceNow Employee
ServiceNow Employee

Hi Leslie,



Surendar and Mandar are recommending simply duplicating the core logic of your code in your Business Rule. This is not a bad choice- it's a fairly simple bit of code you're using, and copying that logic is a quick way to achieve the goal of validating the date server-side during an insert or update operation.



An alternative which would allow you to re-use the code would be to split this into 2 Script Includes- one which GlideAjax can call, and another to do the actual work.



I would propose something along the lines of:


(Client-callable Script Include)


var ClientDateTimeUtils = Class.create();


ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      validateCalendarDate: function() {


              //Client date


              var userDate = this.getParameter('sysparm_userDate');


           


              if (MyDateTimeUtils.validateFormat(userDate))


                      return true;



          return("ERROR: Format must be yyyy-mm-dd");


  },



  type : 'ClientDateTimeUtils'


});



(The actual utility collection)


var MyDateTimeUtils =   {


      validateFormat: function(user_date) {


              //Check date format will match one of the available system dates


              var ymdDash = /^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/; //yyyy-mm-dd


             


              return ymdDash.test(user_date)


      }


};



(An example GlideAjax invocation)


var ga = new GlideAjax("ClientDateTimeUtils");


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


ga.addParam('sysparm_userDate',"2014-01-01");


ga.getXML(function(response) {console.log(response.responseXML.documentElement.getAttribute("answer"))});



(An example Business Rule using the utility function)


function onBefore(current, previous) {


      var userDate = current.getValue("my_date_field");


      if(!MyDateTimeUtils.validateFormat(userDate)) {


              gs.addInfoMessage("ERROR: Date dormat must be yyyy-mm-dd");


              current.setAbortAction(true);


      }


}




Are you using a Date field to gather the required info? It should be sending the data in the correct format already.


Anurag Tripathi
Mega Patron
Mega Patron