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

Oke clear, no worries.
 
There are some solid GlideRecord examples also on the developer site:
 
Just for deleteRecord(), base code could be:
var now_GR = new GlideRecord('sys_user_grmember');
now_GR.addEncodedQuery('<your_encoded_query>');
now_GR.setLimit(1);
now_GR._query();

if(now_GR._next()) {
now_GR.deleteRecord();
}
- or - 
var now_GR = new GlideRecord('sys_user_grmember');
now_GR.addQuery('<field>', '<value>');
now_GR.addQuery('<field>', '<value>');
now_GR.setLimit(1);
now_GR._query();

if(now_GR._next()) {
now_GR.deleteRecord();
}
 
Two examples, one with regular addQuery, one with addEncodedQuery. Both good, no performance difference or something, just a preference. I like to use addEncodedQuery.
 
Notice that I applied .setLimit(1). This is a good practice when you are perform GlideRecord query and after only 1 record. This is also good for the other people who replied here and did not apply this: this is a good practice from performance point of view.
 
Also I applied _query instead of query and _next instead of next, both is just a personal preference of mine to automatically do this as some tables do have literally a field called "query" or "next", in such cases you run into issues with your GlideRecord query.
 
One of the replies here also applied encodedQuery, nice example you could look at though don't just copy it. Why? The example contains a bad practice: dotwalking to the sys_id... thats unnessary and eats performance.
So the example of:
grGroupMember.addEncodedQuery('group.sys_id=' + group_sys_id + '^user.sys_id=' + user_sys_id);
Should be:
grGroupMember.addEncodedQuery('group=' + group_sys_id + '^user=' + user_sys_id);
In your case I think (I don't know since I can't verify or test your situation :-)):
grGroupMember.addEncodedQuery('group=' + current.variables.role + '^user=' + current.variables.requested_for);
 
Just play around with it, might it not work immediately just reply and what you tried. Personally I always also try to debug when something is not working immediately. For example adding log statements, this way you can find the issue within seconds/minutes instead of being stuck for hours.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

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