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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

send the email notification via Before delete BR on sys_user_grmember table and not via scheduled job

Reason being by the time the event is processed the record may not exist as it is deleted

Regards
Ankur

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

Hi Ankur,

Can we just send the list of users for whom the group is deleted.Not the group details 

Thanks,

Karan

Hi,

yes why not your notification is on Group member table you can include that detail of user and group in email body

Regards
Ankur

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

Hi Ankur,

So,I need to create an event on Group member table and even the notification right.

Then can you please help me the code of calling the event so that when it called in notification,the details of the users who are removed are sent to one particular group.

 

Thanks,

Karan