-
Notifications
You must be signed in to change notification settings - Fork 678
Trigger Event when someone is added to a list #1615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SapphicFire
merged 3 commits into
ServiceNowDevProgram:main
from
TechnologistTim:Business-Rule
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
Business Rules/Trigger Event when a member is added to a list/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Scenario: You are wanting to send a notification to someone whenever they are added to a list of people. | ||
|
||
Application: Create an event in the event registry. Then, create a business rule. Set the name and table, advanced = true. | ||
|
||
When to run: | ||
When = after, update = true, conditions = watch list (replace with applicable field name) | changes | ||
|
||
Advanced tab: insert the snippet |
36 changes: 36 additions & 0 deletions
36
...er Event when a member is added to a list/Trigger an event when user is added to field.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(function executeRule(current, previous /*null when async*/) { | ||
|
||
// Ensure the 'watch_list' (replace with applicable field name) field has been modified | ||
if (current.watch_list != previous.watch_list) { | ||
|
||
// Split the current and previous watch lists into arrays | ||
var newWatchers = current.watch_list.split(','); | ||
var oldWatchers = previous.watch_list ? previous.watch_list.split(',') : []; | ||
|
||
// Identify the newly added users to the watch list | ||
var addedUsers = newWatchers.filter(function (user) { | ||
return oldWatchers.indexOf(user) === -1; | ||
}); | ||
|
||
// Loop through the added users to trigger the event for each | ||
addedUsers.forEach(function(userID) { | ||
var email; | ||
var firstName; | ||
|
||
// Try to get the user record by user ID (sys_id) | ||
var userGr = new GlideRecord('sys_user'); | ||
if (userGr.get(userID)) { | ||
firstName = userGr.first_name; | ||
email = userGr.email; | ||
} else { | ||
|
||
// If no user record is found, assume the userID is an email address | ||
email = userID; | ||
firstName = "Team"; | ||
} | ||
|
||
// Trigger the event (replace "new_member") with the current case and user information | ||
gs.eventQueue('new_member', current, email, firstName); | ||
}); | ||
} | ||
})(current, previous); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In future, look to use getters such as
getValue('field')
instead of getting the GlideElement each timeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the tip.