Add or remove member from the group

Michael51
Tera Guru

Hello All,

I have an requirement to Automate request for Add or remove member to group Item ,  we have two list collector fields 

one is for Adding members and the other one for removing members , here my challenge is sometimes if user is already included in the group we need to Auto complete the request by adding work notes as "request already exists " in the group  , need your help in understanding how we can tackle this Scenario 

 

I have following script for adding and removing members from the group for run script 

var group = current.variables.group_name;

var usersToAdd = current.variables.addmemberlist; // give correct variable name here

var usersToRemove = current.variables.removememberlist;
 
var grp1 = new GlideRecord('sys_user_grmember');
grp1.addQuery('group',group);
grp1.addQuery('user',usersToRemove);
grp1.query();
while(grp1.next())
{
grp1.deleteMultiple();
}

var arr = usersToAdd.toString().split(',');

for (var i = 0; i < arr.length; i++) {
    var addRec = new GlideRecord('sys_user_grmember');
    addRec.addQuery("user", arr[i]);
    addRec.addQuery("group", group);
    addRec.query();
    if (!addRec.hasNext()) {
        gs.info("getting into if loop");
        addRec.initialize();
        addRec.user = arr[i];
        addRec.group = group;
        addRec.insert();
    }
}

can you please give some suggestion how we can develop this kind of automation 

@Ankur Bawiskar @jaheerhattiwale 

25 REPLIES 25

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael51 

you can ensure you handle this situation before submitting the catalog item

1) in the add group list collector show those groups only which user doesn't belong to

2) in the remove group list collector show only those groups to which user belongs to

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar ,

you mean to do restriction in the Ref qualifier or any other? can you help me with some reference?

@Michael51 

correct

apply advanced ref qualifier on both the list collector and show groups as per what I mentioned above.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Can I have sample reference ankur @Ankur Bawiskar