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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Neha,



you have an error in your script



current.sys_id should not be within quotes



Also check you are comparing the sys_id with number column, use proper column there



I checked in my demo instance. There is a field 'task' which refers Incident table etc so I have updated the code as well



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


var inc = new GlideRecord('incident');


inc.addQuery('task', current.sys_id); // use this now and check


//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

FWIW, there's no 'task' field on the incident (or task) table.



This is likley more what you need:



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


        var inc = new GlideRecord('incident');


        inc.addQuery('sys_Id', current.task); // use this now and check


        inc.addQuery('active', false);


        inc.query();



while(inc.next())


        inc.u_survey_state = true;


        inc.setWorkflow(false);


        inc.autoSysFields(false);


        inc.update();


})(current, previous);


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);


Thank You Chuck,


This works.



Regards,


Neha