Returning Random Records Using GlideRecord

//Returning Random Records Using GlideRecord

Returning Random Records Using GlideRecord


had a request from a client recently to generate a random list of records from a given table (specifically the Configuration Item table). These items would be used as a pool of records for a random audit of the records in that table. I don’t think this will be used all that often but I figured I’d throw it out here so that people could use it if they needed it. Here’s the code…

Return Random Records Script
This script is set up to return an array containing the names of 5 randomly-selected records from the ‘cmdb_ci’ table. You can set the ‘tbl’ and ‘returnNum’ variables to customize the number of records and the table to query from. You may also choose to modify the GlideRecord query to limit the scope of the query.

var tbl = 'cmdb_ci'; //Query from this table
var returnNum = 5; //Return this number of records in the array
var answer = new Array();

//Set up the GlideRecord query
var rec = new GlideRecord(tbl);
   //Get the number of records queried
   var numRecs = rec.getRowCount();
   //Ensure number of records is not less than number to return
   if(numRecs < returnNum){
     returnNum = numRecs;
   //Return 'returnNum' number of random records in an array
   for(i=0; i < returnNum; i++){
      //Get a random number between 0 and 'numRecs'
      var randomNumber = Math.floor(Math.random()*numRecs);
      //Set the row number to 'randomNumber'
      //Add the random record to the array
gs.log(answer.join()); //Log the output
return answer; //Return the array
By | 2018-07-09T15:00:07-06:00 September 2nd, 2010|Categories: Scripting|Tags: |6 Comments

About the Author:


  1. Stephanie October 29, 2016 at 6:30 am - Reply

    Where would I put this script? I created a business rule on the cmdb_ci table but it doesn’t seem to do anything. I appreciate your help.

    • Mark Stanger October 31, 2016 at 7:53 am - Reply

      That depends entirely on what you want to do with the output of the script. As a general rule, you would want to store the script in the place where you trigger the action for the script to execute. A business rule probably isn’t the right place for most use cases I can think of because that runs when a particular record is inserted or updated to the database. A scheduled job or something similar probably makes more sense but it really depends on what you are trying to accomplish.

  2. Tyrone May 18, 2017 at 12:24 pm - Reply

    I am running this script in a scoped application and receiving the following: Function setLocation is not allowed in scope. Any Suggestions?

    • Jim Pisello July 14, 2017 at 10:37 am - Reply

      Hi Tyrone,

      There are several Glide methods that are not supported in Scoped Applications. You can learn more about APIs for scoped apps by going to and clicking the API menu.

      • Daniel Oderbolz November 7, 2017 at 12:59 am - Reply

        Dear Jim

        Can you be a bit more specific on that?
        I am having the problem that some script include that I call from a dynamic filter that used to run on Helsinki no longer runs on Istanbul.
        (It runs just fine in a background script). I have a feeling, we have exactly this problem (that we call a function that is not allowed from a scoped application and somehow dynamic filters are now treated like a scoped application).
        Service Now could not figure it out yet…


  3. Daniel Oderbolz November 7, 2017 at 12:56 am - Reply

    Dear Mark,
    this is cool code, thanks!
    One small thing tough: I think you haven an off-by-1 isssue in the code.
    You will have records from 0 to numRecs-1 and not to numRecs.
    The problem is unlikely to occur (when the Random Number Generator returns 1), but I think you should account for it.


Leave A Comment