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

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

Brad Bowman
Kilo Patron
Kilo Patron

You should always convert sys_ids to a string using .toString() or getValue to ensure you are getting the sys_id for that iteration through the loop instead of the memory location

x = gr2.getValue('sys_id');

Ankur Bawiskar
Tera Patron
Tera Patron

@Lom 

try this

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

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Shivalika
Mega Sage
Mega Sage

Hello @Lom 

 

It's while loop logic which is affecting the x and y results. 

 

if (i == 1) {

    x = gr2.sys_id;

    i++;

}

if (i == 2) {

    y = gr2.sys_id;

   

I++

}

 

 

Make use of else if(I==2) while putting value of y. 

 

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=0WynLcOw

NeEISQCY