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.

Update Field using Business Rule

nehamendon
Kilo Contributor

Hello,

I created a new field (True/False) on Incident form to be updated when, on Task Survey table the "State" field changes to "completed".

the script is as follows:

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

var inc = new GlideRecord('incident');

inc.addQuery('number', 'current.sys_id');

//inc.addQuery('number','CHG0502941');

gr.addQuery('active', 'false');

inc.query();

while(inc.next())

inc.u_survey_state = 'true';

inc.setWorkflow(false);

inc.autoSysFields(false);

inc.update();

})(current, previous);

However, the above script doesn't work when using "current.sys_id" for number field.

If I hardcode the number value in the script then field value on Incident form changes to "True".

Does anyone have an idea on how to fix this ?

Thanks & Regard,

Neha

1 ACCEPTED SOLUTION

Slightly cleaner version:



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


        var inc = new GlideRecord('incident');


        if (inc.get(current.task)) {


                  inc.u_survey_state = true;


                  inc.setWorkflow(false);


                  inc.autoSysFields(false);


                  inc.update();


        }


})(current, previous);


View solution in original post

7 REPLIES 7

Anais Geisen
Tera Expert

Hi,



In table Task Survey, you have only the "task" field.


So, you should access the number field value with "current.task.number" and replace it in your query.



Hope this helps,



Regards


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Neha,



One more update in the script as suggested above.



you can use either of the addQuery



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


var inc = new GlideRecord('incident');



inc.addQuery('sys_id', current.task); // use this if you want to query based on sys_id



OR



//inc.addQuery('number', current.task.number); // use this if you want to query based on number



//inc.addQuery('number','CHG0502941');


gr.addQuery('active', 'false');


inc.query();



while(inc.next())


inc.u_survey_state = 'true';


inc.setWorkflow(false);


inc.autoSysFields(false);


inc.update();


})(current, previous);



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Yes, you can use either one of those queries. The first is a touch faster because dot-walking requires data retrieval to get the task record and inspect the number field, whereas current.task is already available in the current record. Why go get more data to do the same thing (unless you MUST have the number for some reason, of course.)