- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 12:30 AM
Tryin to crate array of sysids. Don't know why its not getting.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 12:12 PM
Hello,
You have to make correction to from gr.sys_id to gr.sys_id.toString(), because in gr.sys_id you are still referring to the same object. Also the best practice is to use the getter here like gr.getUniqueValue().
See the correction below:
var users = [];
var gr = new GlideRecord('sys_user');
gr.query();
while(gr.next()){
users.push(gr.sys_id.toString());
//users.push(gr.getUniqueValue());
}
gs.info(users);
Thank you,
Mahesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 06:04 PM - edited 12-24-2024 06:04 PM
Hello @ObuM
Thank you for marking my response helpful.
As per the new community policy, you can mark multiple accepted solution.
Can you please mark the solution as accepted, it will help future readers to locate the solution easily in community.
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 08:53 AM - edited 12-24-2024 08:53 AM
Best practice is to use getters and setters rather than direct dot-walking...as dot-walking returns pointers to objects. If you frequently find yourself adding "toString()" to your code, chances are you're just not coding to return the right data types. I would suggest that the code you posted doesn't align with best practices.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 07:26 AM - edited 12-24-2024 07:46 AM
Hello @ObuM
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 users.push(gr.sys_id); ->
users.push(gr.getValue('sys_id'));
This converts sys_id to a string, which avoids the reference issue and prevents all the array elements from being updated simultaneously.
Here is the updated script:
var gr = new GlideRecord('sys_user');
gr.query();
var users = [];
while(gr.next()){
users.push(gr.getValue('sys_id'));
}
gs.info(users);
This will return the sys_id of all the records in sys_user table.
"If you found my answer helpful, please like and mark it as an "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
12-24-2024 08:51 AM
To do one better, you can do gr.getUniqueValue() to get sys_id.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 12:12 PM
Hello,
You have to make correction to from gr.sys_id to gr.sys_id.toString(), because in gr.sys_id you are still referring to the same object. Also the best practice is to use the getter here like gr.getUniqueValue().
See the correction below:
var users = [];
var gr = new GlideRecord('sys_user');
gr.query();
while(gr.next()){
users.push(gr.sys_id.toString());
//users.push(gr.getUniqueValue());
}
gs.info(users);
Thank you,
Mahesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 06:04 PM - edited 12-24-2024 06:04 PM
Hello @ObuM
Thank you for marking my response helpful.
As per the new community policy, you can mark multiple accepted solution.
Can you please mark the solution as accepted, it will help future readers to locate the solution easily in community.
Thank You
Juhi Poddar