Query is returning the same sys_id of the records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 10:55 AM
Hello all,
When i was running the below query it was returning the one sys_id mulitple times.
var cr = new GlideRecord('cmdb_ci_computer');
cr.addQuery('install_status', 1);
cr.setLimit(5);
cr.query();
var arraylist = [];
while(cr.next())
{
arraylist.push(cr.sys_id);
//cr.setValue('install_status',7);
//cr.update();
}
arraylist.join();
Output:
5cf599bf1b3ebd1cc8e521f1604bcb08,5cf599bf1b3ebd1cc8e521f1604bcb08,5cf599bf1b3ebd1cc8e521f1604bcb08,5cf599bf1b3ebd1cc8e521f1604bcb08,5cf599bf1b3ebd1cc8e521f1604bcb08.
Can you please some one say what I was missing?
Thanks in Advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 10:58 AM
Update the script as below and check if it works:
var cr = new GlideRecord('cmdb_ci_computer');
cr.addQuery('install_status', 1);
cr.setLimit(5);
cr.query();
var arraylist = [];
while(cr.next())
{
arraylist.push(cr.getUniqueValue());
//cr.setValue('install_status',7);
//cr.update();
}
gs.log(arraylist.toString()); // for logs
//arraylist.join();
If this solution helps you then, mark it as accepted solution ✔️ and give thumbs up 👍!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 11:10 AM
Hello @Nagendra Babu32
It seems like you're encountering an issue where all values in the array are being updated because sys_id is likely a reference to an object, rather than a primitive value like a string. When you push an object reference to an array and then modify it, all elements holding that reference also change.
By converting sys_id to a string before pushing it, you ensure that a copy of the value (instead of the reference) is stored. Modify the line arraylist.push(cr.sys_id) ->
arraylist.push(cr.sys_id + "");
This converts sys_id to a string, which avoids the reference issue and prevents all the array elements from being updated simultaneously.
"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2024 09:45 PM - edited 10-03-2024 11:56 AM
@debendudas' update would work, but there is no explanation as to why it would (which is very important). @Juhi Poddar lays it out nicely, but I would suggest a slight change by using the built-in getValue() method which automatically returns a string:
arraylist.push(cr.getValue("sys_id"));
The getUniqueValue() method suggested by @debendudas works because it also returns a string, but always the sys_id of the record. You do not have to specify the column you are interested in.
This article shows what is happening the way you had it setup: TNT: The Importance of Using "getValue()" when Getting Data from a GlideRecord