How to send notification once whenever member added to group.

Vinay88
Kilo Contributor

I want to send notification to group manager only once if we added/removed number of users as group's member.

 

For example :

1. If i have added 4 users as group member then it should only trigger one notification to group manager and if  added 4 more then also it should trigger one notification only.

2. If we removed 2 then also it will trigger one notification.(Right now its triggering for each user addition/deletion.)

Any help is appreciated.

Thanks,
Vinay

1 ACCEPTED SOLUTION

Yeah, I tried it without the time restriction, and still couldn't get it to not add the second event, or even change the State so that it didn't trigger the notification, so it's probably best to get rid of that business rule and let the events get added.

I hope I'm not leading you down another rabbit hole, but I stumbled upon something that seems to be getting us closer.  In your business rule that is calling the event on deletion, change the call to this

gs.eventQueue('group.membership.delete', current, current.group.name, current.user.name, 'group.membership.delete');

That last parameter will put it into a queue so it won't process.  Incidentally, without this I was seeing the event error out and the notification not trigger since it was trying to process on a record that no longer exists, which makes sense I guess.  In any event, now we haven't done anything to trigger a notification, but we have captured each name (or ID, or whatever you want) that was deleted from each group, and the date/time stamp, so that seems like progress.

Does the group manager need to get the notification right away?  If not, you could have a scheduled job that runs nightly against the sysevent table and pushes all of the names into an array for each group for that day, then calls a new event which is what would cause the notification to trigger.  The scheduled job would change the state and/or the queue so that they don't get picked up the next time, so you could run it every hour or whatever and not mess with time ranges.

View solution in original post

13 REPLIES 13

Hello Brad,

 

I have tried it, but seems like its not working.

On Before Business rule,

var y = new GlideDateTime(current.sys_created_on.getDisplayValue());
y.addSeconds(-6);
var time1 = y.getTime().toString().split(" ");
var query1 = "javascript:gs.dateGenerate('" + y.getDate().toString() + "','" + time1[1].toString() + "')";

y.addSeconds(12);
var time2 = y.getTime().toString().split(" ");
var query2 = "@javascript:gs.dateGenerate('" + y.getDate().toString() + "','" + time2[1].toString() + "')";

var gr = new GlideRecord('sysevent');
gr.addEncodedQuery("name=group.membership.delete^table=sys_user_group^parm1=" + current.parm1 + "^sys_created_onBETWEEN" + query1 + query2);
gr.query();
if (gr.next()) {
current.setAbortAction(true);
}

 

Please look into it and let me know what's the issue.

In my instance I created a business rule after delete on sys_user_grmember with this script

gs.eventQueue('group.membership.delete', current, current.group.name, current.user.name);

The second parm might come in handy later.  Then I created the event registry with the same event name on the sys_user_grmember table - I don't know if it matters but Queue is empty, and for information I noted it is Fired by Business Rule.  Then I created a business rule before insert on sysevent with your script, changing the encoded query to table = sys_user_grmember, but I don't think that matters as long as it agrees with the event.  I also changed the seconds to -180 and 180 to give myself a bigger window.  I deleted 2 group members and saw 2 events added to the event log, so I added gs.info lines in the BR to log query1 and query2.  This is where I get confused - I'm not great on dates and times, so I'll just tell you what was logged.

I deleted the 2 group members at 6:52:22 pm EDT.

query1 = javascript:gs.dateGenerate('2020-04-06','10:50:05')

query2 = javascript:gs.dateGenerate('2020-04-06','10:53:05')

In the event log list view when I filter on created on or after the first deletion record (which displays as 06:52:22PM), I get

sys_created_on>=javascript:gs.dateGenerate('2020-04-06','18:52:22')

This timezone/system time difference, or whatever is must be why the encoded query is not finding the record that exists for the same created_on time since everything else looks correct.

 

Hello Brad,

For me timezone is same and getting same filter query only but still all event created.(5 event created and using script getting all 5 events only).

Run below background script after event created.(added any one of event sys_id in below script)

var current = new GlideRecord('sysevent');

current.addEncodedQuery("sys_id=e3336291db4c94108f9ab5b96896197b");//sys_id of event
current.query();
if (current.next()) {
var y = new GlideDateTime(current.sys_created_on.getDisplayValue());
//var y = new GlideDateTime(current.sys_created_on);
y.addSeconds(-10);
var time1 = y.getTime().toString().split(" ");
var query1 = "javascript:gs.dateGenerate('" + y.getDate().toString() + "','" + time1[1].toString() + "')";
gs.print('created on ' + current.sys_created_on.getDisplayValue());
gs.print('start looking before 10 sec');
gs.print('query1 ' + query1);
var gr = new GlideRecord('sysevent');
gr.addEncodedQuery("name=group.membership.delete^table=sys_user_group^parm1=" + current.parm1);
gr.addQuery("sys_created_on>=" + query1);
gr.orderByDesc('sys_created_on');
gr.query();
while (gr.next()) {
gs.print('sys_id ' + gr.sys_id);
//current.setAbortAction(true); //abort
}
}

Output:

*** Script: created on 04-07-2020 09:15:14
*** Script: start looking before 10 sec
*** Script: query1 javascript:gs.dateGenerate('2020-04-07','09:15:04')
*** Script: sys_id 6f33a691db4c94108f9ab5b9689619e2
*** Script: sys_id bf33a691db4c94108f9ab5b9689619e5
*** Script: sys_id ff33a691db4c94108f9ab5b9689619e8
*** Script: sys_id a7336291db4c94108f9ab5b968961977
*** Script: sys_id e3336291db4c94108f9ab5b96896197b

 

Still facing the same issue.

Yeah, I tried it without the time restriction, and still couldn't get it to not add the second event, or even change the State so that it didn't trigger the notification, so it's probably best to get rid of that business rule and let the events get added.

I hope I'm not leading you down another rabbit hole, but I stumbled upon something that seems to be getting us closer.  In your business rule that is calling the event on deletion, change the call to this

gs.eventQueue('group.membership.delete', current, current.group.name, current.user.name, 'group.membership.delete');

That last parameter will put it into a queue so it won't process.  Incidentally, without this I was seeing the event error out and the notification not trigger since it was trying to process on a record that no longer exists, which makes sense I guess.  In any event, now we haven't done anything to trigger a notification, but we have captured each name (or ID, or whatever you want) that was deleted from each group, and the date/time stamp, so that seems like progress.

Does the group manager need to get the notification right away?  If not, you could have a scheduled job that runs nightly against the sysevent table and pushes all of the names into an array for each group for that day, then calls a new event which is what would cause the notification to trigger.  The scheduled job would change the state and/or the queue so that they don't get picked up the next time, so you could run it every hour or whatever and not mess with time ranges.

Vinay88
Kilo Contributor

@Chuck Tomasi @Pradeep Sharma @Max Pham @Kreg Steppe @Andrew Pishchulin @Andrew Albury-Dor @jacebenson @nathanfirth @gauravchoudhury @sumeet.n @Mark Scott @Chuck Tomasi @travistoulson 

 

Please provide your input for notify manager when removing the member's from group.

 

My requirement is, if i am removing 4-5 remembers from the group then it should trigger only 1 notification to group manager with member's details.