Event trigger after users are removed from the groups

KARAN24
Tera Contributor

Hi Team,

 

I have a schedule job which is removing the  users from groups and removing the roles.

But I want to send a notification to a particular group with the User Name and Group details from which they are removed.

I am struck with how to trigger an event and send notification.

Here is my script:

 


var roles=new GlideRecord('sys_user_has_role'); // go to user role table
roles.addEncodedQuery('role!=2831a114c611228501d4ea6c309d626d^user.last_login_time<javascript:gs.beginningOfLast3Months()');
roles.query();
while(roles.next())
{
var SID=roles.user;

var userMem=new GlideRecord('sys_user_grmember');
userMem.addQuery('sys_id',SID);
userMem.query();
var removedFromGroups = [];
if (userMem.next()) {

userMem.deleteRecord();


removedFromGroups.push(userMem.group.getDisplayValue());


}




gs.print(userMem.user + " " + removedFromGroups);


gs.eventQueue('notification_to_group', current,);


}



roles.deleteMultiple();




1 ACCEPTED SOLUTION

@KARAN

Hi,

event, notification on gr member table correct

trigger the event from before delete BR

gs.eventQueue('event_name', current, 'recipient', current.user.name + '@' + current.group.name);

Now you are sending the user and group in event parm2

in email script you can split it with @ and then print the user and group

var user = event.parm2.split('@')[0];

var group = event.parm2.split('@')[1];

I believe I have provided enough help for your question.

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

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Hi Ankur,

 

Can you please help me with the below issue;

Mine script is working,but I am struck where should I call the event and what should be in that code,what all parameters should be there.I want to send only the list of users who have been removed from group to a particular individual.

Here is my code:

 

 


var us1=new GlideRecord('sys_user');
us1.addEncodedQuery('last_login_time<javascript:gs.beginningOfLast3Months()^last_login_timeISNOTEMPTY^active=true');
us1.query();
while (us1.next()) {

 

 

var memberRec = new GlideRecord('sys_user_grmember');

//memberRec.addEncodedQuery('group!=62e0f859db4bc410b2537a76f396194b^ORgroup!=491c1e2fdb7e4010b2537a76f3961992');

memberRec.addQuery('user',us1.sys_id);
memberRec.query();
memberRec.deleteMultiple();
while (memberRec.next()) {

 

}

gs.eventQueue('notification_to_group', '');

var userRoleRec = new GlideRecord('sys_user_has_role');

 

userRoleRec.addQuery('user', us1.sys_id);

userRoleRec.query();

while (userRoleRec.next()) {

userRoleRec.inherited = 'false';

userRoleRec.update();

}

 

 

var grRemove1 = new GlideRecord('sys_user_has_role');

grRemove1.addEncodedQuery('role!=2831a114c611228501d4ea6c309d626d');

grRemove1.addQuery('user',us1.sys_id);

grRemove1.query();

while (grRemove1.next()) {

grRemove1.deleteMultiple();

}

 

 

 

 


}

 

 

 

@KARAN

Hi,

event, notification on gr member table correct

trigger the event from before delete BR

gs.eventQueue('event_name', current, 'recipient', current.user.name + '@' + current.group.name);

Now you are sending the user and group in event parm2

in email script you can split it with @ and then print the user and group

var user = event.parm2.split('@')[0];

var group = event.parm2.split('@')[1];

I believe I have provided enough help for your question.

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

Regards
Ankur

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