need help in business rule

2022_ServiceNow
Tera Expert

Hi, I have a business rule which runs on the task table when the particular task state based on the item is changed to closed complete. There is a field in that task 'current.request_item.configuration_item' which is referenced to computer system table. So when task is closed, need to update the field in that record. This is not getting updated.

 

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

    var comp= current.request_item.configuration_item;
    var cs= new GlideRecord('u_cmdb_ci_computersystem');
    cs.addQuery('name', comp);
    cs.query();
    while (cs.next()) {
        cs.setValue('install_status', 'Return');
        cs.update();
    }
})(current, previous);

 

  @Ankur Bawiskar @Amit Gujarathi 

2 ACCEPTED SOLUTIONS

What's output of comp in the log, if its reference column then it must be sys_id of record from table [ [u_cmdb_ci_computersystem]

 

replaced the cs.addQuery('sys_id', comp);

 

 

 

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

    var comp= current.request_item.configuration_item;
    var cs= new GlideRecord('u_cmdb_ci_computersystem');
    cs.addQuery('sys_id', comp);  // if comp value is sys_id then apply the query on column sys_id instead of name 
    cs.query();
    while (cs.next()) {
        cs.setValue('install_status', 'Return');
        cs.update();
    }
})(current, previous);

 

 

 

 

 

 

 

View solution in original post

So its returning sys_id , perfect ..also going inside the while loop ( as TEST1 printed in log ).

 

are you sure that , install_status which is Integer type select box with choice values, I checked the sys_choice table for this install_status,  all values are numeric. Check the same and use the numeric value instead of 'Return'.  

 

AshishKMishra_0-1698872719551.png

 

View solution in original post

13 REPLIES 13

Brad Bowman
Mega Patron

Add a line within the while block to save the update:

 

cs.update();

If it's still not working, add some log lines to confirm the correct record is returned in your query.

 

 

 

@Brad Bowman  

I have added this log before while loop, and it's returning "[object GlideRecord] sys_id TEST"

 

 

gs.log(cs + comp + 'TEST');

 

 

Is it correct way to get the value of the dot-walked field using current? 

var comp= current.request_item.configuration_item;  

 

Inside the while block you need to determine if the correct record is present, so try something more like

 

gs.log(cs.name + ' TEST');

 

after the while statement.  Is the 'name' field on your custom table a reference to cmdb_ci?

 

Since you're getting a sys_id in the log of comp, it must work if that's the correct sys_id, but OOTB there is no field named request_item on the task table.

@Brad Bowman  , it's not getting into the while statement. 

The name field is the 'computer system name' in the 'u_cmdb_ci_computersystem' table. It is not reference field