Variable values being overwritten incorrectly by while loop

karstenvan
Mega Expert

Hi all,

I have a weird issue that probably has a simple answer but I'm not seeing it.

I have a query running against a lookup table to pull in the SYS_ID for the various records in the lookup table.

I'm declaring the target variables before the query loop.

During the loop, I can see that the proper values are being found and logged.   (greedid, yellowid, redid).   Each is finding the proper sysID

However after the loop ends,   showing the variable values outside of the loop returns the sys_id for greenID in all 3 variables.

How can this be happening?

Here is the code chunk in question....

gs.log('starting lookups for status values');

      var cgr = new GlideRecord('u_lookup_project_status');

      var redid = 'red';

      var yellowid = 'yel';

      var greenid = 'green';

      gs.log('initial vals are ' + redid + yellowid + greenid);

      cgr.addNotNullQuery('Name');

      cgr.query();

      gs.log('starting loop');

      while (cgr.next()) {

    gs.log('inside the loop');
    if (cgr.u_name == 'Green') {
    greenid = cgr.sys_id;
    gs.log('found green   ' + cgr.sys_id + '     ' + greenid);
    }
    if (cgr.u_name == 'Yellow') {
    yellowid = cgr.sys_id;
    gs.log('found yellow   ' + cgr.sys_id + '     ' + yellowid);
    }
    if (cgr.u_name == 'Red') {
    redid = cgr.sys_id;
    gs.log('found red   ' + cgr.sys_id + '     ' + redid);
   
    }

      }

      gs.log('green is '+ greenid);

      gs.log('yel is '+ yellowid);

      gs.log('red is '+ redid);

1 ACCEPTED SOLUTION

bernyalvarado
Mega Sage

Hi Karsten,



That's because the sys_id is a reference. So you're assigning greenid, yellowid, redid to the same memory position which holds the reference to the last record you iterated within your loop.



Use a .toString() instead so that you can pass the actual sys_id value instead of the reference.



For instance:



Change line 18 to:



greenid = cgr.sys_id.toString();


Change line 28 to:



yellowid = cgr.sys_id.toString();   


Change line 38 to:



redid = cgr.sys_id.toString();


Thanks,


Berny


View solution in original post

4 REPLIES 4

Raju Koyagura
Tera Guru

In line number 8, you used cgr.addNotNullQuery('Name')



but in each if condition you compared with u_name.



You should use element name u_name in line number 8 as well like below


cgr.addNotNullQuery('u_name')





Robert Beeman
Kilo Sage

Are you saying that the logs from lines 21, 31, 41 shows something different than lines 48-50? Is the greenid from line 21 the same as 48? How many records are in u_lookup_project_status?


bernyalvarado
Mega Sage

Hi Karsten,



That's because the sys_id is a reference. So you're assigning greenid, yellowid, redid to the same memory position which holds the reference to the last record you iterated within your loop.



Use a .toString() instead so that you can pass the actual sys_id value instead of the reference.



For instance:



Change line 18 to:



greenid = cgr.sys_id.toString();


Change line 28 to:



yellowid = cgr.sys_id.toString();   


Change line 38 to:



redid = cgr.sys_id.toString();


Thanks,


Berny


Thanks all!   Berny's suggestion fixed the issue.   The way you described it makes sense.



I was very confused that variable log statements were spitting out different values inside the loop vs. outside (as Robert pointed out).   Making the values going into the variables explicit made it all work fine.  



Thanks again!