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

How to remove users, roles and set the group fileds empty from workflow run script

Brahmi Pandla
Tera Guru

@Ankur Bawiskar ,

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 = '';  //Reference filed
    gr.default_assignee = '';  //Reference filed
    gr.type = '';  //Reference filed
    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-1735222896168.png

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Brahmi Pandla 

what does this variable u_existing_group hold?

Is it a group sysId?

If yes then update as this

var grpId = current.variables.u_existing_group;
 
// set the group fileds empty
 
var gr = new GlideRecord('sys_user_group');
gr.addQuery('sys_id',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', grpId);
grRemoveMember.query();
while(grRemoveMember.next()){
    grRemoveMember.deleteRecord();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

@Brahmi Pandla 

Something similar

var groupRoleRec = new GlideRecord('sys_group_has_role');
groupRoleRec.addQuery('group', grpId);
groupRoleRec.query();
while(groupRoleRec.next()){
    groupRoleRec.deleteRecord();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

8 REPLIES 8

@Brahmi Pandla 

Something similar

var groupRoleRec = new GlideRecord('sys_group_has_role');
groupRoleRec.addQuery('group', grpId);
groupRoleRec.query();
while(groupRoleRec.next()){
    groupRoleRec.deleteRecord();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Sandeep Rajput
Tera Patron
Tera Patron

@Brahmi Pandla 

 

grRemoveMember.addQuery("user", 'IN', grpId);

This condition looks incorrect as you are adding a condition for the user field and passing the sys_id of the group. Due to this the query will fail and deletion will never take place.

@Sandeep Rajput 

 

Thanks for response but your script is not working

 

@Brahmi Pandla I didn't share the script, I just shared the reason why the script was not working.