Skip to content

Commit c07ffe7

Browse files
Trigger Event when someone is added to a list (#1615)
1 parent 25796ae commit c07ffe7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Scenario: You are wanting to send a notification to someone whenever they are added to a list of people.
2+
3+
Application: Create an event in the event registry. Then, create a business rule. Set the name and table, advanced = true.
4+
5+
When to run:
6+
When = after, update = true, conditions = watch list (replace with applicable field name) | changes
7+
8+
Advanced tab: insert the snippet
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
// Ensure the 'watch_list' (replace with applicable field name) field has been modified
4+
if (current.watch_list != previous.watch_list) {
5+
6+
// Split the current and previous watch lists into arrays
7+
var newWatchers = current.watch_list.split(',');
8+
var oldWatchers = previous.watch_list ? previous.watch_list.split(',') : [];
9+
10+
// Identify the newly added users to the watch list
11+
var addedUsers = newWatchers.filter(function (user) {
12+
return oldWatchers.indexOf(user) === -1;
13+
});
14+
15+
// Loop through the added users to trigger the event for each
16+
addedUsers.forEach(function(userID) {
17+
var email;
18+
var firstName;
19+
20+
// Try to get the user record by user ID (sys_id)
21+
var userGr = new GlideRecord('sys_user');
22+
if (userGr.get(userID)) {
23+
firstName = userGr.first_name;
24+
email = userGr.email;
25+
} else {
26+
27+
// If no user record is found, assume the userID is an email address
28+
email = userID;
29+
firstName = "Team";
30+
}
31+
32+
// Trigger the event (replace "new_member") with the current case and user information
33+
gs.eventQueue('new_member', current, email, firstName);
34+
});
35+
}
36+
})(current, previous);

0 commit comments

Comments
 (0)