Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client side script for setting mandatory variables (g_form.setMandatory)

kopp820
Mega Guru

I need to use the same client side script in both a UI action and a UI policy. I'm wondering if it's more efficient to use a script include, or to just type the code twice in both places. My gut says the former, but I can't get my script include and glide ajax to work to return the code. The code is just g_form.setMandatory('variables.cat_variable', true); for about 5 variables. In this code, i'm only showing 2 for example. Thoughts?

 

Script include example:

Client checkbox is true

var MandatoryFields = Class.create();
MandatoryFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
     mandatoryFields: function(){
                 return "g_form.setMandatory('variables.example1', true); g_form.setMandatory('variables.example2', true);";
           },
     });

 

Client side UI action & UI Policy (Run scripts = true)

           var ga = new GlideAjax('MandatoryFields');
     ga.addParam('sysparm_name','mandatoryFields');


           ga.getXML(EnforceMandatoryFields);
           
           function EnforceMandatoryFields(response) {
                 var answer = response.responseXML.documentElement.getAttribute("answer");
                 
           }

 

Note: The business case for this is certain catalog variables need to be filled out before the user can close a specific catalog task.

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Here's what you can do to "share" code:



1. create an onLoad Client Script with the code that you want to run from the UI Action and Policy:



function onLoad() {


}


function u_setMandatoryFields() {


    g_form.setMandatory('variables.example1', true);


    g_form.setMandatory('variables.example2', true);


}



The onLoad function does nothing at all, but it allows the u_setMandatoryFields function to be defined.



2. Now you can call u_setMandatoryFields() from both places.



GlideAjax is used when you want to actually return values for use somewhere.


View solution in original post

4 REPLIES 4

Jim Coyne
Kilo Patron

Here's what you can do to "share" code:



1. create an onLoad Client Script with the code that you want to run from the UI Action and Policy:



function onLoad() {


}


function u_setMandatoryFields() {


    g_form.setMandatory('variables.example1', true);


    g_form.setMandatory('variables.example2', true);


}



The onLoad function does nothing at all, but it allows the u_setMandatoryFields function to be defined.



2. Now you can call u_setMandatoryFields() from both places.



GlideAjax is used when you want to actually return values for use somewhere.


Very helpful, thank you!! I knew there had to be a better way.


Jim Coyne (CompuCom)


Swarnadip Roy


You are welcome.


swarnadip
ServiceNow Employee
ServiceNow Employee

Hi Kalley,



Jim's solution should work good.


For the second point - how to call the function in Client Script from UI Policy,


Please see below


Screen Shot 2014-11-20 at 1.06.57 PM.png


Hope this helps