why array printing one number multiple times instead of printing every record number under query??

KM SN
Tera Expert

var abc = [];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('caller_id=77ad8176731313005754660c4cf6a7de');
gr.query();


while(gr.next()){


abc.push(gr.number);
}
gs.print(abc);

ManiHouse1_0-1715074227931.png

can anyone give me explanation in laymen terms??

 

 

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Replace

abc.push(gr.number);

with

abc.push(gr.number.toString());

Hi Jaspal, Thank you for response

already i got to know to print exact result either by adding toString() or by using getvalue method but the expecting is why its behaving so? what is the reason behind it?

toString() will store the memory location in the array, not the actual record value, so you'll get an array of all of the same record value if you don't.

Use toString() on the end of object value assignments to prevent printing same values. 

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @KM SN ,

 

try this code :

 

var abc = [];
var gr = new GlideRecord('incident');
gr.addQuery('caller_id', '77ad8176731313005754660c4cf6a7de');
gr.query();
while (gr.next()) {
    abc.push(gr.getValue('number')); 
}

gs.info(abc.join(', ')); 

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect