Random selection of records on a table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2016 07:43 AM
Hi,
Need some help with creating a button (UI page or macro) that would randomly select a record on a custom table.
Any advise would be greatly appreciated.
Thanks,
Maria

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2016 07:47 AM
Hi Maria,
First, read in all the sysIDs of the records you want. Something like this:
Standard disclaimer: The following code is untested, requires review and potential modifications.
var incList = [];
var rec = new GlideRecord('incident');
rec.addQuery('active', true);
rec.query();
while (rec.next()) {
incList.push(rec.getValue('sys_id'));
}
var random = Math.floor((Math.random() * incList.length));
var sys_id = incList[random];
You now have the sys_id of a random record in a list.
NOTE: All code is untested and meant to be modified to suit your needs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2016 08:57 AM
Thanks Chuck. That worked!
One other question. So if I wanted to display the value of a reference field instead of the sys_id, do I just change the line incList.push(rec.getValue('sys_id'));?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2016 01:35 PM
If you want the display value (eg. INC0010345) instead of a sys_id, change it to
incList.push(rec.getDisplayValue());
Q: Were you looking for the entire record or just a random label?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2016 02:36 PM
Hi Chuck,
Tried that but nothing happened. The record contains a reference field (sys_user) and I would like the name to display instead of the sys_id of that user record.
Maria