Send notification to a particular group about the output of schedule job

KARAN24
Tera Contributor

Hi Team,

I am executing the script through schedule job to remove the user from groups and roles.

I am getting a count of users through log statement (like how many users have been removed),so the same details that is the count,I want to send to a particular group as an email.How can I achieve that.

 

var count=0;

var inclist=[];
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);
while (memberRec.next()) {
count++; // count number of users who is having role and didn't log-in from last 60 days
memberRec.deleteMultiple(); // remove roles from the role table
gs.log("Hello! User "+count+ " is removed from group" );


}

 Thanks,

Karan

3 REPLIES 3

ryan_pope
Mega Guru

Once you've finished looping through your query, you can generate an event, and initiate a notification based on that event. 

1. Create an event in the Event Registry

2. In your script, after the while loop ends, add gs.eventQueue([event name],current,count) , or something along those lines.

3. Create a new notification triggered off of an event, and select the name of the event you just created. Set the recipient to the group that needs to receive the email. If you use the formatting I provide in step 2, I believe you should just need to use $ {event.parm1} to call that count value.

 

Event Registry | ServiceNow Developers

KARAN24
Tera Contributor
Hi Ryan, I tried it. ga.eventQueue('namegroup',memberRec,count); And in the notification I am calling the mail script with parm 1 ,but the issue is it is triggering multiple emails. Thanks

If it's triggering multiple times, it's likely because it's still within one of your while loops. Can you share your code again, with the new line?