The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Random selection of records on a table

Maria DeLaCruz
Tera Guru

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

5 REPLIES 5

Chuck Tomasi
Tera Patron

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.

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


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?


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