The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Client Script to set value true if field from another is true

taddparris
Kilo Expert

Need help with a client script to set a field true, if a field on another table related to a change request is true.   Looking at the Affected CI's to see if Tagged for DR is true.  

What I need is:

If "u_tagged_dr" is true on the "task_ci" table and matches the sysid of the Change Request

Set value of field "u_core_or_critical_ci_affected" to true

Here is what I have so far and it is not working

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

               

      var cc = new GlideRecord('task_ci');

  cc.addQuery('task_ci', g_form.getValue('u_tagged_dr', true));

  cc.query();

                if (cc.next())   {

                      g_form.setValue('u_core_or_critical_ci_affected', 'true');

        }

}

Thanks,

11 REPLIES 11

Chuck Tomasi
Tera Patron

Are you sure you want to do this in a client script? This doesn't seem like a use case where immediate feedback on the records is requirement when the checkbox is clicked. What's more, if someone accidentally checks it, the process is going to execute regardless.



This seems more like a use case of an AFTER business rule, which makes this a bit easier technically.



Something like this:


Name: Update Affected CIs


Table: Change Request


When: After


Insert: true


Update: true


Advanced; true


Condition: current.u_tagged_dr.changesTo(true)   // Not sure if this is the condition WHEN you want to run the script. Adapt as necessary


Script:


(function executeRule(current, previous /*null when async*/) {


        var ci = new GlideRecord('task_ci');


        ci.addQuery('task', current.sys_id);


        ci.addQuery('u_tagged_dr', true);


        ci.query();


        while (ci.next()) {


                  ci.u_core_or_critical_ci_affected = true;


                  ci.update();


        }



})(current, previous);



Reference:


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


I just re-read the requirements and understand that u_core_or_cirtical_ci_affected is on the change request record.



In that case, this is a more effective script:



(function executeRule(current, previous /*null when async*/) {


        var ci = new GlideAggregate('task_ci');


        ci.addQuery('task', current.sys_id);


        ci.addQuery('u_tagged_dr', true);


        ci.addAggreate('COUNT');


        ci.query();


        while (ci.next()) {


                  var count = ci.getAggregate('COUNT');


                  if (count > 0) {


                            current.u_core_or_critical_ci_affected = true;


                            ci.update();


                  }


        }



})(current, previous);


I currently have this working as a BR.   The issue I described above is the reason I am looking at trying a Client Script.



Will add   why I want to try this as a Client Script and not a Business Rule for a certain reason.   Currently we have two business rules which work off each other and they are working.   BR #1 sets field "u_core_or_critical_ci_affected" to true, and BR #2 checks risks conditions which one of those conditions is if "u_core_or_critical_ci_affected" is set to true.   It requires two updates of a record to match the condition and modify the risk, my thought is a Client Script will update the field before the Risk Condition check occurs and only require a single update.


Hi Tadd,



For what it's worth, business rules can run in a specific order, you get to set using the Order field. If you have one that needs to run first, set the order lower (100) and a second one (200) will follow. Before rules always run before After rules.



Best practices suggest, if you can do this using server scripts like BRs, use that first.



Also, follow up update, my BR should run as a BEFORE, not after. I thought you were modifying related records (done in after rules), not the current (change) record done in before rules.


I tried to change the order but currently it is set to run "Before" not "After".   Tried to run it after and it is not working.   Here is the BR.... There are no Conditions set.



function onBefore(current, previous) {


                current.u_core_or_critical_ci_affected=false;


                var gr = new GlideRecord('task_ci');


                gr.addQuery('task',current.sys_id);


                var pk =   gr.addQuery('u_tagged_dr', true);


                pk.addOrCondition('u_p1_eligible',true);


                gr.addQuery('u_p1_eligible',true);             //recent addition


                gr.setLimit(1);


                gr.query();


                if (gr.next())   {


                      current.u_core_or_critical_ci_affected=true;


        }


else {



current.u_core_or_critical_ci_affected=false;


}


      }