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.

How to Add or Remove Users in Group.(Using Workflow).

Para5
Tera Guru

1. I have created one catalog item "Add Remove client portal Admnistrators".
2.On this catalog item added two variables:
    1.Contact to Add - When the User move in Selected, Automatically add to the 'Client portal group'
    2.Contact to Remove - When User move in Available,Automatically remove from the 'Client portal group'.
3.My requirement is adding or removing users from the 'Client portal group'. I want to implement this using workflow.
   Thank You.

find_real_file.png

1 ACCEPTED SOLUTION

Hi,

try this

var usersToAdd = current.variables.contact_to_add.toString().split(','); // give correct variable name here

var usersToRemove = current.variables.contact_to_remove.toString(); // give correct variable name here

var rec = new GlideRecord('sys_user_grmember');
rec.addQuery("group.name", "Client portal group");
rec.addQuery("user", 'IN', usersToRemove);
rec.query();
while(rec.next())
    rec.deleteRecord();

for(var i in usersToAdd){
    var rec1 = new GlideRecord('sys_user_grmember');
    rec1.addQuery("user", usersToAdd[i]);
    rec1.addQuery("group.name", "Client portal group");
    rec1.query();
    if(!rec1.hasNext()){
        rec1.initialize();
        rec1.user = usersToAdd[i];
        rec1.setDisplayValue('group', "Client portal group");
        rec1.insert();
    }
}

regards
Ankur

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

View solution in original post

14 REPLIES 14

Hi Ankur,

Thanks for response

var grpId = current.variables.u_existing_group;

//set the group fileds empty
var gr = new GlideRecord('sys_user_group');
gr.addQuery('group',grpId);
gr.query();
if(gr.next()){
    gr.parent = '';
    gr.default_assignee = '';
    gr.type = '';
    gr.active = 'false';
    gr.update();
}
//Remove group members
var grRemoveMember = new GlideRecord('sys_user_grmember');
grRemoveMember.addQuery('group', grpId);
grRemoveMember.addQuery("user", 'IN', grpId);
grRemoveMember.query();
while(grRemoveMember.next()){
    grRemoveMember.deleteRecord();
}
BrahmiPandla_0-1735222228152.png

 

 

The below fields are reference fields
gr.parent = '';
    gr.default_assignee = '';
    gr.type = '';
 
Could you please suggest where  i did mistake

@Brahmi Pandla 

Can you post a new question for this and share the script and tag me there as this is an old thread?

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

Hi Ankur

I am trying to delete the group members and set the group values empty with below workflow run script

but not working

var grpId = current.variables.u_existing_group;
 
// set the group fileds empty
 
var gr = new GlideRecord('sys_user_group');
gr.addQuery('group',grpId);
gr.query();
if(gr.next()){
    gr.parent = '';
    gr.default_assignee = '';
    gr.type = '';
    gr.active = 'false';
    gr.update();
}
//Delete group members
var grRemoveMember = new GlideRecord('sys_user_grmember');
grRemoveMember.addQuery('group.name',Test13025896);
grRemoveMember.addQuery("user", 'IN', grpId);
grRemoveMember.query();
while(grRemoveMember.next()){
    grRemoveMember.deleteRecord();
}
 

@Brahmi Pandla 

Can you post a new question for this and share the script and tag me there as this is an old thread?

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