Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Using array to add to multiple groups

Cindy Sim
Tera Expert
I am trying to use array to add user to multiple group in AD. Example: If user select 'xyz group' and 'qrs group'  to the variable 'what access is needed'. User should be added to 'xyz_group', 'qrs_group' respectively and same with any other groups selected. I am using following script but it's only giving me one group. any suggestions?
 
var abc = [current.variables.what_access.getDisplayValue.toString(',')];
var user= current.request.requested_for.name;
var action = current.variables.add_remove;
var answer = [];
workflow.scratchpad.group = getGroup(abc);
workflow.scratchpad.action = action;
function getGroup(abc) {
    var result = [];
    for (var i = 0; i < abc.length; i++) {
        if (abc[i] == 'xyz group') {
 
            result.push('xyz_group');
        }
        if (abc[i] == 'qrs group') {
 
            result.push('qrs_group');
        }
}
    }
    return result;
}
1 REPLY 1

Aniket Chavan
Tera Sage
Tera Sage

Hello @Cindy Sim ,

Please give a try to the modified script below and see how it works for you.

var accessValues = current.variables.what_access.getDisplayValue().split(','); // Split the comma-separated string into an array
var user = current.request.requested_for.name;
var action = current.variables.add_remove;
var answer = [];

workflow.scratchpad.groups = getGroups(accessValues);
workflow.scratchpad.action = action;

function getGroups(accessValues) {
    var result = [];

    for (var i = 0; i < accessValues.length; i++) {
        var access = accessValues[i].trim(); // Trim any extra spaces

        if (access == 'xyz group') {
            result.push('xyz_group');
        } else if (access == 'qrs group') {
            result.push('qrs_group');
        }
        // Add more conditions for other groups as needed
    }

    return result;
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket