Workflow to Loop through an array of groups not working

tsutherland
Kilo Sage

I am working on a sub-flow in our employee offboarding to change AD group owner (managedBy) to the offboarded employee's manager. The process works, but I am stuck on iterating properly through the array and ChatGPT has not been helpful. 🙂

 

In my initial script, I set the domain control and the appropriate variables of the employee and manager and query for a list of the groups owned by the employee. This all works fine. In addition, I set the group counter and failure flag like so

 

workflow.scratchpad.group_counter = 0;
workflow.scratchpad.group_remove_failure = false;

 

The second part, I have cobbled together from a similar process someone else wrote and it is building the array of groups and the JSON of values to change

 

	var groupArray = workflow.scratchpad.groups;
	var values = groupArray.split(",");
	var length = values.length;
	
	gs.log('Groups are ' + groupArray, 'Tbot'); //this is working
	gs.log('Array is ' + values, 'Tbot'); //this is working
	gs.log('Length is ' + length, 'Tbot' ); //this is working

for (var i = 0; i < values.length; i++) {
	var groups = values[i];
}
workflow.scratchpad.array_length = values.length;
workflow.scratchpad.group_owner = values;
workflow.scratchpad.group_change = groupArray;
gs.log('var groups is ' + groupArray, 'Tbot'); //this returns the array, same as above
workflow.scratchpad.failed_groups = [];

//build JSON with AD name and value to be sent to AD
var attributeNames = [];
var attributeValues = [];
attributeNames.push('managedBy');
attributeValues.push(workflow.scratchpad.owner);

//build JSON
workflow.scratchpad.adjson = '{';
for (jsonIndex = 0; jsonIndex < attributeNames.length; jsonIndex++){
	if(attributeValues[jsonIndex] != ''){
		workflow.scratchpad.adjson = workflow.scratchpad.adjson + '"' + attributeNames[jsonIndex] + '" : "' + attributeValues[jsonIndex] + '"';
	} else {
		continue;
	}
	if((jsonIndex + 1) != (attributeNames.length)){
		workflow.scratchpad.adjson = workflow.scratchpad.adjson + ',';
	}
}
workflow.scratchpad.adjson = workflow.scratchpad.adjson + '}';

gs.log('JSON is the following; ' + workflow.scratchpad.adjson, 'Tbot'); //this is working

 

This mostly works, as you can see in the comments that it returns what I expect it to return in the logs.

Then I run this script:

 

workflow.scratchpad.group_to_change = workflow.scratchpad.group_owner[workflow.scratchpad.array_length];
gs.log('Group to Change is ' + workflow.scratchpad.group_to_change, 'Tbot'); //returns undefined

 

And the logs are returning "undefined".  ChatGPT wanted me to put '-1' after 'workflow.scratchpad.array_length' and when I did that, it returned only the last group in the array, though the rest of the workflow to change ownership worked, but only on that one group.

 

Here is the whole workflow:

tsutherland_0-1711130579481.png

In my IF block, I have this code

 

 answer = ifScript();

  function ifScript() {
	  gs.log('group that will be changed ' + workflow.scratchpad.group_to_change,'tbot');//returns undefined
	  
     if (workflow.scratchpad.group_counter < workflow.scratchpad.array_length) {
        return 'yes';
     }
     return 'no';
  }

 

And if the change is successful, I have it increment the group counter.

 

workflow.scratchpad.group_counter ++;

 

If there is a failure, I have it log the failed group and then increment the group counter

 

workflow.scratchpad.failed_groups.push(workflow.scratchpad.group_to_change.toString());

gs.log('failed group ' + workflow.scratchpad.group_to_change.toString(), 'Tbot');//returns undefined
workflow.scratchpad.group_remove_failure = true;

 

I know my incrementing isn't working somewhere, I just don't know what to change. Any ideas/suggestions/solutions are much appreciated!

0 REPLIES 0