Email to newly added Watchlist users and CC manager

Pranesh_RP
Tera Contributor

I got a requirement. Where I need to send notification to only newly added user in watchlist and also CC his manager in it. Please Do help.

10 REPLIES 10

Swarup Patra
Kilo Guru

Sure, here are the steps you can follow to achieve this:

1. Create a new field on the task table to store the previous value of the watchlist. This field can be of type 'glide_list'.

2. Create a business rule that runs 'before update' on the task table. In this business rule, compare the current value of the watchlist with the stored previous value. If there is a new user, store this user in a separate field on the task table. Also, update the previous value of the watchlist with the current value.

Sample code for the business rule:


(function executeRule(current, previous /*null when async*/) {
var oldWatchlist = previous.watch_list.toString().split(',');
var newWatchlist = current.watch_list.toString().split(',');
var addedUsers = [];
for(var i = 0; i < newWatchlist.length; i++) {
if(oldWatchlist.indexOf(newWatchlist[i]) === -1) {
addedUsers.push(newWatchlist[i]);
}
}
current.u_added_users = addedUsers.toString();
current.u_previous_watch_list = current.watch_list.toString();
})(current, previous);


3. Create a notification that triggers when the field for the newly added user is not empty. In this notification, set the recipient as the newly added user and CC his manager.

Sample code for the notification:


(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', current.u_added_users.toString());
gr.query();
while(gr.next()) {
gs.eventQueue('custom.event', current, gr.sys_id, gr.manager);
}
})(current, previous);


In the above code, 'custom.event' is the name of the event that triggers the notification. The third parameter is the sys_id of the newly added user and the fourth parameter is the sys_id of his manager.

4. In the notification, use the event parameter 1 and event parameter 2 to get the sys_id of the newly added user and his manager respectively.

Please note that the above solution assumes that the manager of the user is stored in the 'manager' field of the 'sys_user' table. If it is stored in a different field, please replace 'manager' with the appropriate field name.


nowKB.com

If you want to know any information about Service Now . Visit to https://nowkb.com/home