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

Get Query Array

traviswarren
Kilo Expert

Hello all,

I am having an issue with my Get query array.   I am attempting to add a security group to the variable workflow.scratchpad.group.   Only one record must be set at a time due to me using this workflow.scratchpad.group variable for a powershell script.  

My intent is to get all matching records in a array then parse it with a split separated by commas.   With position, update workflow.scratchpad.group for the first record, then the second record and so on until there is no more records left.   I have a checker to identify when there is no more records. which is not documented below.

Here is my code below.

Any help is appreciated Thanks.

workflow.scratchpad.dlPosition = 0;

getGroup(workflow.scratchpad.dlPosition);

function getGroup(position) {

var gr = new GlideRecord('u_user_role_privilege');

if(gr.get('u_location', workflow.scratchpad.locationOfGroup[position])){

workflow.scratchpad.group = gr.u_dn;

workflow.scratchpad.dlPosition++;

}

}

var groupArr = [];

var list;

var rolePrivilege = new GlideRecord('u_user_role_privilege');

rolePrivilege.addQuery('u_location',workflow.scratchpad.location);

rolePrivilege.query();

while(rolePrivilege.next())

{

  groupArr.push(rolePrivilege.u_location.toString());

}

for(var count=0; count< groupArr.length; count++)

{

  list = groupArr[count];

  workflow.scrathpad.locationOfGroup = list.split(',');

}

1 ACCEPTED SOLUTION

Edwin,



Thanks everyone for putting your minds together on this matter.   I will show you what I believe is working now.



Determine array


var groupArr = [];


var list;


var rolePrivilege = new GlideRecord('u_user_role_privilege');


rolePrivilege.addQuery('u_location',workflow.scratchpad.location);


rolePrivilege.query();


while(rolePrivilege.next())


{


  groupArr.push(rolePrivilege.u_ad_group.toString());


}


workflow.scratchpad.locationOfGroup = groupArr;



Get Query


getGroup(workflow.scratchpad.dlPosition);


function getGroup(position) {


var gr = new GlideRecord('u_user_role_privilege');


if(gr.get('u_ad_group', workflow.scratchpad.locationOfGroup[position])){


workflow.scratchpad.group = gr.u_dn;


workflow.scratchpad.dlPosition++;


}


}



ifScript Any more Records?



answer = ifScript();




function ifScript() {


    if (workflow.scratchpad.locationOfGroup.length > workflow.scratchpad.dlPosition) {


          return 'yes';


    }


    return 'no';


}



I also found a spelling error too which could have been another issue.



Thanks again!


View solution in original post

8 REPLIES 8

Zach Biewend1
Giga Expert

I'm not sure I completely understand the problem, but I see a couple issues with this script.



In line 5 you call locationOfGroup[], but that doesn't get assigned until line 24. so it will probably produce 'undefined' when called in line 5.



Also, the for loop at line 21 properly iterates through groupArr[], but it never does anything with locationOfGroup[] once it sets it. So all at once you will set locationOfGroup many times and never do anything with the data stored in it.



Again, I'm not 100% certain of the way the code works, but perhaps move line 2 down to line 25 so it executes each time the for loop iterates?



Hope this helps!


Hello,



Agree with Zachary, but I think line two should be outside of the loop. Also I'm not sure if the for loop and the while loop are going to achieve what you want, are you storing a string separated by commas in u_location? I'm not sure why you are splitting the u_location.


Zachary,



Thanks for your quick response.   I will explain more in depth since I did not explain what my issue was.



I am using the code below in a run script to determine what the array sys_ids are.   In the request item, the user selects a location which is stored in worflow.scratchpad.location.   I query the u_location field on the role privilege table to get results of the u_location pushed to the array called groupArr.   I then take the array and separate the sys_ids with split and set it to workflow.scratchpad.locationOfGroup.




var groupArr = [];


var list;


var rolePrivilege = new GlideRecord('u_user_role_privilege');


rolePrivilege.addQuery('u_location',workflow.scratchpad.location);


rolePrivilege.query();


while(rolePrivilege.next())


{


  groupArr.push(rolePrivilege.u_location.toString());


}


for(var count=0; count< groupArr.length; count++)


{


  list = groupArr[count];


  workflow.scrathpad.locationOfGroup = list.split(',');


}




I have another run script below.   My goal is to get the first array of location sys_id set above and match the u_location field on the role privilege table.   When i find the match then set workflow.scratchpad.group to the DN of the record. Then add dl position to get the next position in the array if there is one.



getGroup(workflow.scratchpad.dlPosition);


function getGroup(position) {


var gr = new GlideRecord('u_user_role_privilege');


if(gr.get('u_location', workflow.scratchpad.listArray[position])){


workflow.scratchpad.group = gr.u_dn;


workflow.scratchpad.dlPosition++;


}


}



Next I am running a powershell script that is adding a user to a group.



Then an ifScript below to see if there is any more groups to add.   Rinse and Repeat.



answer = ifScript();




function ifScript() {


    if (workflow.scrathpad.locationOfGroup.length > workflow.scratchpad.dlPosition) {


          return 'yes';


    }


    return 'no';




I hope this explanation provides a little more insight.



Thanks again.


Hi Travis,



If I understand correctly I think the problem is that you are not storing an array of sys_ids in workflow.scrathpad.locationOfGroup.



The variable list only contains a sys_id when you split it so even though you are doing this in a loop at the end you are going to have an array of only one sys_id.



Maybe you can get rid of the for loop, an push the values directly to the workflow.scrathpad.locationOfGroup.



Does this make sense? Sorry if I'm not understanding correctly.