Passing an Array to GlideRecord Query in the Workflow

VAIGAI_KOTHANDA
Giga Contributor

HI,

We need to pass an array value to the GlideRecord Query in the Workflow. We have used the following code to pass an array value.


var answer = [];
var app = current.functions.getDisplayValue();
var array = app.split(",");
//var array1 = new ArrayUtil().unique(array);
var grBS = new GlideRecord('u_ecare_choicevalues');

for (var i=0; i < array.length; i++) {
grBS.addQuery('u_name', array[i]);

grBS.query();

while(grBS.next())
  {
  answer.push(grBS.approval_group);
  }
}

Here the problem is , its taking the first value and its not taking running for the other values.

Can you please help us fix this issue.

1 ACCEPTED SOLUTION

HI Harish,



Thanks for the help.



I made some changes to the code and it is working now.



var answer = [];
var govap = current.functions.getDisplayValue();
gs.log('function1:'+govap);
var arrayVal = govap.split(",");
len = arrayVal.length;

for (var i=0; i<=len; i++) {

var grBS = new GlideRecord('u_ecare_demand_choicevalues');
//grBS.addQuery('u_name', arrayVal[i]);
grBS.addQuery('u_name','IN', arrayVal[i]);
grBS.query();

while(grBS.next())
{
answer.push(grBS.approval_group);
gs.log('function2:'+ arrayVal[i]);

}
gs.log('function3:'+ arrayVal[i]);
}




I queried through the for loop and got "grBS.addQuery('u_name','IN', arrayVal[i]);" instead of grBS.addQuery('u_name', arrayVal[i]); , therefore it is working now.




Thanks for the help Harish Murikinati and Mani kanta Korada .




View solution in original post

11 REPLIES 11

Hi Harish,



I have posted the above requirement and yes i tried your code, it didn't give any go.



If you can look in to the requirement and let me know what changes can we make in the code to make it work.



Thanks.


Keep alert and check either log is priting or not



var answer = [];
var app = current.functions.getDisplayValue();


gs.log('App :'+app)// send me what its printing exactly
var arrayval = app.split(",");



//var array1 = new ArrayUtil().unique(array);




var len = arrayval.length;


gs.log('Length :'+len);


for (var i=0; len>i; i++)


{


gs.log('count :'+i);


      var grBS = new GlideRecord('u_ecare_choicevalues');
      grBS.addQuery('u_name', arrayval[i]);


      grBS.query();


      while(grBS.next())
    {


gs.log('Inside while loop :'+grBS.approval_group);
          answer.push(grBS.approval_group);
    }


}



Send all logs info what logs printing.


HI Harish,



Thanks for the help.



I made some changes to the code and it is working now.



var answer = [];
var govap = current.functions.getDisplayValue();
gs.log('function1:'+govap);
var arrayVal = govap.split(",");
len = arrayVal.length;

for (var i=0; i<=len; i++) {

var grBS = new GlideRecord('u_ecare_demand_choicevalues');
//grBS.addQuery('u_name', arrayVal[i]);
grBS.addQuery('u_name','IN', arrayVal[i]);
grBS.query();

while(grBS.next())
{
answer.push(grBS.approval_group);
gs.log('function2:'+ arrayVal[i]);

}
gs.log('function3:'+ arrayVal[i]);
}




I queried through the for loop and got "grBS.addQuery('u_name','IN', arrayVal[i]);" instead of grBS.addQuery('u_name', arrayVal[i]); , therefore it is working now.




Thanks for the help Harish Murikinati and Mani kanta Korada .




Hi Harish,



I am back again.



I am stuck here when the Group approvals are generated.



All the approvals fetched from the Group approvals become No longer required.



We have written a business rule (insert and Update) on sysapprover_approval table, which changes them to requested but it is not allowing them to change to any other state i.e Approved or No Longer Required.



approveDuplicateApproval();



function approveDuplicateApproval(){


     


                var app = new GlideRecord('sysapproval_approver');


             


             


                app.addQuery('sysapproval', current.sysapproval);


             


                //app.addQuery('approver', current.approver);


                var enc = "state=approved^ORstate=not_required";


          app.addEncodedQuery(enc);


       


              // app.addQuery('wf_activity.workflow_version', current.wf_activity.workflow_version);


                app.query();


                if(app.next()){


                     


                        current.state = 'requested';


                   


  current.update();


                }


     


}



Please help us on this.



Thanks


manikorada
ServiceNow Employee
ServiceNow Employee

You can use the same script with out passing in a array something like:



var answer = [];
var app = current.functions.getDisplayValue();
var grBS = new GlideRecord('u_ecare_choicevalues');


grBS.addQuery('u_name', 'IN', app);


grBS.query();


while(grBS.next())
  {
  answer.push(grBS.approval_group);
  }