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

Script result

Lom
Tera Contributor

Hello,

I have an issue with a script and I don't know what wrong.

The script returns the same value for x and y instead of two different values. Can you help?

 

var x,y;
var req, item;
var i=1;

var gr2 = GlideRecord('sc_req_item');
gr2.addQuery('requested_for', '86f01750db3cdf00269cfd431d9619d3');
gr2.addQuery('cat_item', 'e660f7838781b1101709a8280cbb35e0');
gr2.orderByDesc('sys_created_on');
gr2.setLimit(2);
gr2.query();

while (gr2.next()) {
    if (i == 1) {
        x = gr2.sys_id;
        i++;
    }
    if (i == 2) {
        y = gr2.sys_id;
        i++;
    }
    gs.info('sys_id=' + gr2.sys_id + '  for=' + gr2.requested_for + '  created=' + gr2.sys_created_on);
}
	


gs.info('id1='+x+'  id2='+y);

and the result is:

*** Script: sys_id=426e27b2c3106e109dd63fc90501312c  for=86f01750db3cdf00269cfd431d9619d3  created=2025-03-19 13:30:36
*** Script: sys_id=27cdab72c3106e109dd63fc905013193  for=86f01750db3cdf00269cfd431d9619d3  created=2025-03-19 13:27:58
*** Script: id1=27cdab72c3106e109dd63fc905013193  id2=27cdab72c3106e109dd63fc905013193

 

why are id1 and id2 igal?

1 ACCEPTED SOLUTION

Nilesh Pol
Tera Guru
Tera Guru

Hi @Lom 

verify with below updated script:

var x, y;
var i = 1;

var gr2 = new GlideRecord('sc_req_item');
gr2.addQuery('requested_for', '86f01750db3cdf00269cfd431d9619d3');
gr2.addQuery('cat_item', 'e660f7838781b1101709a8280cbb35e0');
gr2.orderByDesc('sys_created_on');
gr2.setLimit(2);
gr2.query();

while (gr2.next()) {
if (i == 1) {
x = gr2.sys_id.toString(); // Ensure string conversion
} else if (i == 2) {
y = gr2.sys_id.toString();
}

gs.info('sys_id=' + gr2.sys_id + ' for=' + gr2.requested_for + ' created=' + gr2.sys_created_on);
i++; 
}

gs.info('id1=' + x + ' id2=' + y);

View solution in original post

11 REPLIES 11

Lom
Tera Contributor

Thank you to all,

it's the combination of 'else if' and toString() that makes it works!!

For clarity, adding an 'else' before the second 'if' is purely optional and a matter of style / preference.  The code will execute the same either way.  Technically, since 'i' can only be 1 or 2 in your scenario, then it should be - shown using the recommended getValue method:

 

while (gr2.next()) {
    if (i == 1) {
        x = gr2.getValue('sys_id');
        i++;
    } else {
        y = gr2.getValue('sys_id');
        i++;
    }
}

 

However, there is nothing wrong with using two separate 'if' statements in your scenario.  Some prefer this approach for readability.  Here's what it would look like using the other recommended method getUniqueValue when storing the sys_id of the retrieved GlideRecord:

 

while (gr2.next()) {
    if (i == 1) {
        x = gr2.getUniqueValue();
        i++;
    }
    if (i == 2) {
        y = gr2.getUniqueValue();
        i++;
    }
}

 

In the first GlideRecord while loop iteration, the second 'if' statement is simply skipped since the condition is not met.  Similarly, during the second while loop iteration, the first 'if' statement is skipped.

@Lom 

Glad to know.

As per new community feature you can mark multiple responses as correct.

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

Hi @Lom 

 

It's great your issue was resolved. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY.

Lom
Tera Contributor

Hi,

I give you the result with the else added, the id1 and id2 are still the same:

var x,y;
var req, item;
var i=1;

var gr2 = GlideRecord('sc_req_item');
gr2.addQuery('requested_for', '86f01750db3cdf00269cfd431d9619d3');
gr2.addQuery('cat_item', 'e660f7838781b1101709a8280cbb35e0');
gr2.orderByDesc('sys_created_on');
gr2.setLimit(2);
gr2.query();

while (gr2.next()) {
    if (i == 1) {
        x = gr2.sys_id;
        i++;
    }
    else if (i == 2) {
        y = gr2.sys_id;
        i++;
    }
    gs.info('sys_id=' + gr2.sys_id + '  for=' + gr2.requested_for + '  created=' + gr2.sys_created_on);
}
	


gs.info('id1='+x+'  id2='+y);

 

*** Script: sys_id=ffd8fc53c3d0ae109dd63fc905013127  for=86f01750db3cdf00269cfd431d9619d3  created=2025-03-20 13:34:26
*** Script: sys_id=a7187853c3d0ae109dd63fc905013151  for=86f01750db3cdf00269cfd431d9619d3  created=2025-03-20 13:31:07
*** Script: id1=a7187853c3d0ae109dd63fc905013151  id2=a7187853c3d0ae109dd63fc905013151