The CreatorCon Call for Content is officially open! Get started here.

How to push GlideRecord query to an array

Pratiksha Lang1
Kilo Sage

How to push GlideRecord query to an array in a Before Query BR

Please find the code below:

I want to push the GlideRecord query to an array. 

Note : I have written this code on  a Before Query BR

 

var answer = [];
var grRole = new GlideAggregate('x_amspi_smdrs_app_role_matrix');
grRole.groupBy('site');
grRole.query();
while (grRole.next()) {
var grInp = new GlideRecord('x_amspi_smdrs_app_input_ola');
grInp.addQuery('site', grRole.site);
grInp.addQuery('role', 'DM');
grInp.query();
while (grInp.next()) {
gs.info('check code');
}

}

11 REPLIES 11

Ian Mildon
Tera Guru

You appear to be missing the syntax to push the query data into the array you declared with var answer = [] and then what to do with the contents of the array

I know @Ian Mildon, but I am not sure how to do it. I want to push both role and site into an array.

Here is a quick BR to gather all "assigned_to" values for a record, add it to an array and then take the contents of the array and populate another field with all the contents.

(function executeRule(current, previous /*null when async*/ ) {

   var taskAssign = []; //create array

   var getAssigned = new GlideRecord('sc_task');
   getAssigned.addQuery('request_item', current.sys_id);
   getAssigned.query();
   while (getAssigned.next()) {
       taskAssign.push(getAssigned.getDisplayValue('assigned_to'));
   }

   current.u_sctask_assigned_all = taskAssign.join(','); //push array contents to list field

})(current, previous);

https://www.codecademy.com/catalog/language/javascript

and start your journey to learning javascript.

Ian Mildon
Tera Guru

Did a little reworking of your script. Can't say if this actually works as I don't have the same tables/values as you; but it should give you some ideas on what to try.

getRoleAndSite();

function getRoleAndSite() {
    var grRole = new GlideAggregate('x_amspi_smdrs_app_role_matrix');
    grRole.groupBy('site');
    grRole.query();
    var answer = [];
    while (grRole.next()) {
        answer.push(grRole.site.toString());
    }

    var arrayUtil = new ArrayUtil();
	var getSite = arrayUtil.unique(answer)

    var grInp = new GlideRecord('x_amspi_smdrs_app_input_ola');
    grInp.addQuery('site', 'IN', getSite);
    grInp.addQuery('role', 'DM');
    grInp.query();
    
    while (grInp.next()) {
        gs.info('check code');
    }
}