How to email only a new user added to the watchlist

JordanDR
Tera Contributor

Hi all,

We have a notification set up to notify users that they have been added to the watchlist, which sends the to the watchlist group. However, when an additional user is added, it sends the notification to everyone on the watchlist, even if they have already received the notification before. Is there a way to configure the notification to make it so that only the person who is added to the existing watchlist gets the notification?

Thanks,

Jordan

1 ACCEPTED SOLUTION

Hi Jordan,

Here is the working code.

Step1 : Create event with name : 'sn_hr_core.watchlist_notification'.

Step 2 : Write After BR on insert, update like below

Condition : 

current.watch_list.changes()

Script :

(function executeRule(current, previous /*null when async*/) {
	var oldwatch_list = [];
	var newwatch_list = [];
	var emailTo = [];
	
	if(previous.watch_list) 
		oldwatch_list = previous.watch_list.split(',');
	
	if (current.watch_list) 
		newwatch_list = current.watch_list.split(',');
	
	if (oldwatch_list.length == 0) 
		emailTo = newwatch_list;
	else {
		for (var i = 0; i < newwatch_list.length; i++) {
			if (oldwatch_list.indexOf(newwatch_list[i]) < 0) 
				emailTo.push(newwatch_list[i]);
		}
	}

	gs.eventQueue('sn_hr_core.watchlist_notification', current, emailTo, gs.getUserDisplayName());
})(current, previous);

Step 3 : Create notification and trigger through event name you created in step 1 and in 'Who will receive' tab just check event Param1 checkbox like below.

find_real_file.png

Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

5 REPLIES 5

Musab Rasheed
Tera Sage
Tera Sage

Hello @Jordan de la Rosa ,

Please go through below link. You can create event and invoke it from BR which in turn call notification. Go through it. Mark my answer as correct or hit like based on impact.

https://community.servicenow.com/community?id=community_question&sys_id=05525761db101fc01dcaf3231f961988

Regards,

Musab

Please hit like and mark my response as correct if that helps
Regards,
Musab

Filipe Cruz
Kilo Sage
Kilo Sage

Hello Jordan,

Here is a possible solution for you:

  • Create a BR in your table, triggered on update that will:
    • pick the previous.watch_list and compare it with the current.watch_list to get the difference between them. Use a code similar to this to get that:

      var arrayUtil = new ArrayUtil();
      var prev_users = previous.watch_list.split(",");
      var current_users = current.watch_list.split(",");
      
      var new_users = arrayUtil.diff(prev_users, current_users);


    • generate an event (called, for example, "notify_new_watchlist_members") and pass the "new_users" array (you need to do a new_users.join(",") to pass it as a string) as param1 of the event
  • Create or reuse a notification to be sent to the new watchlist users. Notify the users that are coming in param1 by setting the flag "Event parm1 contains recipient".
    find_real_file.png

This should do the trick for you!

If this answer is relevant for you, please mark it as correct/helpful.

Thanks,

Filipe

Hey Felipe,

 

Thanks for the response. I'm trying this out and am just confused by the part where I have to generate a new event. Where exactly am I supposed to pass the "new_users" array? Am I to do this in the business rule I previously created with the code above or within the event somehow?

 

Any help would be appreciated!

Thanks,

Jordan

Hi Jordan,

Here is the working code.

Step1 : Create event with name : 'sn_hr_core.watchlist_notification'.

Step 2 : Write After BR on insert, update like below

Condition : 

current.watch_list.changes()

Script :

(function executeRule(current, previous /*null when async*/) {
	var oldwatch_list = [];
	var newwatch_list = [];
	var emailTo = [];
	
	if(previous.watch_list) 
		oldwatch_list = previous.watch_list.split(',');
	
	if (current.watch_list) 
		newwatch_list = current.watch_list.split(',');
	
	if (oldwatch_list.length == 0) 
		emailTo = newwatch_list;
	else {
		for (var i = 0; i < newwatch_list.length; i++) {
			if (oldwatch_list.indexOf(newwatch_list[i]) < 0) 
				emailTo.push(newwatch_list[i]);
		}
	}

	gs.eventQueue('sn_hr_core.watchlist_notification', current, emailTo, gs.getUserDisplayName());
})(current, previous);

Step 3 : Create notification and trigger through event name you created in step 1 and in 'Who will receive' tab just check event Param1 checkbox like below.

find_real_file.png

Please hit like and mark my response as correct if that helps
Regards,
Musab