Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Random Sample Set of incidents per member of a group

Ross Walker
Tera Contributor

Hi All
I am trying to prduce a random list of incidents (5) per member of a resolver group

Im using Data Sampling Techniques by drjohnchun
so i have a script include

var randomSampleRecords = [];   // this is in rhino.sandbox context in Scripted Filter; otherwise in global
function randomSample(tableName, encodedQuery, sampleSize, fieldName) {
   if (randomSampleRecords.length) return randomSampleRecords;   // in Scripted Filter, force to run only once
   try {
       //var gr = new GlideRecordSecure(tableName);   // enforce ACL; GlideRecordSecure undefined in Scripted Filter in Helsinki
       var isScriptedFilter = !this.GlideRecordSecure;   // use the fact that GlideRecordSecure is undefined in Scripted Filter
       var gr = new GlideRecord(tableName);
       if (!gr.isValid()) throw 'Invalid table name "' + tableName + '".';
       if (!gr.canRead()) throw 'No permission to read from "' + tableName + '".';   // test ACL for table
       fieldName = fieldName || 'sys_id';   // default to sys_id
       if (gr.getElement(fieldName) == null) throw 'Field "' + fieldName + '" not found.';
       if (!(sampleSize > 0)) throw 'Sample size must be a positive integer.';  
       // get population
       if (encodedQuery) gr.addQuery(encodedQuery);
       gr.query();   // to getRowCount()
       var population = gr.getRowCount();
       if (!population || population < sampleSize) throw 'Total number of rows ' + population + ' is less than sample size ' + sampleSize + '.';
       // throw dice and get a random sample
       var offsets = [], records = [];
       while (records.length < sampleSize) {
           var offset = Math.floor(Math.random() * population);   // 0 <= offset < population
           if (indexOf(offsets, offset) >= 0) continue;   // dupe offset, so rethrow dice
           offsets.push(offset);
           if (offsets.length >= population) break;   // tried entire population
           gr.chooseWindow(offset, offset + 1);   // works in global & scoped
           gr.query();
           if (gr.next()) {
               var value = gr.getElement(fieldName).toString();
               if (indexOf(records, value) < 0) records.push(value);
           }
       }
       if (isScriptedFilter) randomSampleRecords = records;   // in Scripted Filter, save randomSampleRecords
       return records;
   }
   catch(e) {
       return 'ERROR: ' + e;   // return error message
   }
   // emulates Array.prototype.indexOf() in older JavaScript
   function indexOf(arr, val) { for (var i = 0; i < arr.length; i++) if (arr[i] == val) return i; return -1; }
}

and i have the randomSample javascript that calls is
if i want it per member of the group the only way i can get it to show the results is running a seperate for each assigned to 
javascript&colon;randomSample('incident','active=false^stateIN6,7^opened_atONLast week@javascript&colon;gs.beginningOfLastWeek()@javascript&colon;gs.endOfLastWeek()^resolved_atONLast week@javascript&colon;gs.beginningOfLastWeek()@javascript&colon;gs.endOfLastWeek()^u_action!=02f061451b7dd410399eea0e6e4bcb27^u_action!=3434c3b61bf16d5099e0315fdc4bcb0d^u_action!=7a5f4256db9cff409a256055ca9619cc^u_action!=18cf4656db9cff409a256055ca96191c^u_action!=3042c73e1bb16d5099e0315fdc4bcb37^assignment_group=b1393c10db46a700a5dacd4d0b961975^assigned_to=30ed1e7bdb01ef40f168e16c0b9619b6', 6,'number')

what i want it to be able to do is run for each member of the assignment group without having to produce a list per person individually 
if i add more than one scripted filter i just get the results from the first one
if i take out the assigned to i just get random 5 from the assignment group
is there any way to combine them so i dont have to do a seperate one for all 37

0 REPLIES 0