The CreatorCon Call for Content is officially open! Get started here.

If statements not working in while loop....

rebecca75
Tera Contributor

The "if" conditions are not working in the while loop.

The current.assignment_group is 'ITSM-Tools and Technology' sys_id and

the previous.assignment_group is 'Cerner-AMS-Careware' sys_id.

As you can see below, it loops twice, the first time the cur_grpname is set to ITSM-Tools and Technology and pre_grpname is still '', which is good. However, the second loop, the cur_grpname is updated as well as the pre_grpname, although the sys_id == the previous.assignment, which is different. So the end result is that the cur_grpname and pre_grpname are the same, which is fine if the previous and current assignment group are the same, but in this case they are not. Ugh...

This should be a quick thing, but for some reason this is stumping me....ideas?

Business rule -

var grpname = new GlideRecord('sys_user_group');

var qc = grpname.addQuery('sys_id', previous.assignment_group);

qc.addOrCondition('sys_id', current.assignment_group);

grpname.query();

var cur_grpname = '';

var pre_grpname = '';

while(grpname.next()) {    

        if (grpname.sys_id == current.assignment_group) {

                cur_grpname = grpname.name;  

        }

        else if (grpname.sys_id == previous.assignment_group) {

                              pre_grpname = grpname.name;  

        }

}

*** Script: count: 2

*** Script: ITSM-Tools and Technology

*** Script: (REB) LOOP: ITSM-Tools and Technology ,

*** Script: Cerner-AMS-CareAware

*** Script: (REB) LOOP: Cerner-AMS-CareAware , Cerner-AMS-CareAware

1 ACCEPTED SOLUTION

dvp
Mega Sage

If you trying to get the current and previous group names you don't need to query the group



instead try this


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


// Add your code here

gs.log('Current ' + current.assignment_group.name);
gs.log('Previous ' + previous.assignment_group.name);


})(current, previous);


View solution in original post

4 REPLIES 4

dvp
Mega Sage

If you trying to get the current and previous group names you don't need to query the group



instead try this


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


// Add your code here

gs.log('Current ' + current.assignment_group.name);
gs.log('Previous ' + previous.assignment_group.name);


})(current, previous);


rebecca75
Tera Contributor

Thank you that worked perfectly and saved me a trip to the server for no reason.


The SN Nerd
Giga Sage
Giga Sage

dvp is spot on. There is no need to query the record, just dot walk.



For future reference, avoid dot walking in while loops, as it returns a GlideElement Object and not a string/number.



Instead:


      if (grpname.getValue('sys_id') == current.getValue('assignment_group') ) {


              cur_grpname = grpname.getValue('name');


      }


      else if (grpname.getValue('sys_id') == previous.getValue('assignment_group') ) {


                              pre_grpname = grpname.getValue('name');


      }



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thank you!



I went with the dot-walking, but someone else in my area said that I should use the "toString();" which also works.


ex. cur_grpname = grpname.name.toString();