The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Take a user out of a group via workflow script

HenryD
Tera Guru

Hello, im doing im WF that once it is completed it has a script activity to take the user that the user selects out of the group, the variable is a reference to the user group table that is called "role".  right now i have a script running but no success yet. is there any easier way of doing this or any suggestions? 

 

var RemoveGroup = new GlideRecord('sys_user_grmember');

RemoveGroup.initialize();

RemoveGroup.user = current.variables.requested_for; //user requested

RemoveGroup.group = current.variables.role; //role = variable referencing group table

RemoveGroup.Remove();

 

var RemoveGroup = new GlideRecord('sys_user_grmember');

RemoveGroup.initialize();

RemoveGroup.user = current.variables.requested_for;//user requested 

RemoveGroup.group = current.variables.role; //role = variable referencing group table

RemoveGroup.RemoveGroup();

 

 

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @HenryD 

Let try my adjustment from your script below.

var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', current.variables.role);
grMember.addQuery('user', current.variables.requested_for);
grMember.query();
if(grMember.next()){
    grMember.deleteRecord();
}

 

If you're using Flow Designer, you can implement it with no code as mentioned by Mark.

Sample

1. Get Requested Item Variables

2. Query to Group Member table with Group and User condition

3. Delete Record from the second step.

Timi_0-1705466933792.png

 

Cheers,

Tai Vu

View solution in original post

6 REPLIES 6

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

I assume taking a user out of a group means removing? So deleting the sys_user_grmember record?

 

You could also do this with zero code (for example Flow Designer). Though responding on your scripting: if this is about deleting, you are nowhere doing any deletions in your script? No deleteRecord() or deleteMultiple(), nothing?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

agree, I am trying to remove the user from the group, i thought remove would be a better option since in my mind it would remove them from the group, so going to try deleteRecord, or what would you suggest? apologies 

I can understand your thinking, though you cannot just make up things 😁 .Remove() is nothing.

 

Also just wondering, how did you come up with this piece of scripting? Because also seeing the .initialize() part, that's used when inserting records, not when quering or doing an update or delete. So also that is incorrect. That's why asking how did you come up with this, so we can understand the path you took and perhaps can educate on a better path to take.

 

I can also just paste some code here that you can just copy, though I don't think you learn that much from just copy/paste all the time.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hello, yes totally understand i had reused code that i used for a another workflow on adding a group after the workflow finishes via script, & i thought it would be the same thing but just removing it now, i now understand it will have to be differently entirely.