- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 04:24 PM
I want to automatically add users to the watchlist field when an update is made by them to a record on the requested item table (only to the records where a custom checkbox field xyz = true)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 09:38 PM
@Community Alums
You can write Before update business rule, below is the script:(Replace xyz with correct field name)
(function executeRule(current, previous) {
// Check if the checkbox field "xyz" is true and the record is being updated
if (current.xyz == true && current.update()) {
var currentUser = gs.getUserID();
// Get the existing watchlist
var watchlist = current.watch_list.toString();
// Check if the current user is not already in the watchlist
if (watchlist.indexOf(currentUser) == -1) {
// Add the current user to the watchlist
watchlist += "," + currentUser;
current.watch_list = watchlist;
current.update();
gs.info("User " + currentUser + " has been added to the watchlist of record " + current.getDisplayValue());
}
}
})(current, previous);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 04:44 PM
Hi RJ8,
you will have to create a business rule wherein you will have to specify:
- what table it will run (for you it sc_req_item)
- when to run ( after update)
- filter condition (xyz is true)
- actions (add users)
Here's an additional info for your guidance:
1. Navigate to Business Rules:
Go to System Definition > Business Rules in the ServiceNow navigation pane.
2. Create a New Business Rule:
Click on New to create a new Business Rule.
3. Define the Conditions:
Define the conditions under which you want this business rule to trigger. In this case, it would be when the 'urgent' field is true.
4. Set the Script:
In the Script section of the Business Rule, you'll need to write a script that adds the requestor to the watch list when the 'urgent' field is true.
Here's a sample script to achieve this:
(function executeRule(current, previous /*null when async*/) {
// Check if the 'urgent' field is true
if (current.urgent == true) {
// Add the requestor to the watch list
current.watch_list = current.requested_for;
}
})(current, previous);
This script checks if the 'urgent' field is true, and if so, it adds the requestor (requested_for field) to the watch list (watch_list field).
5. Save the Business Rule:
After defining the script, save the Business Rule.
6. Test the Business Rule:
Test the Business Rule to ensure it's functioning as expected. Create a test request item with the 'urgent' flag set to true and verify that the requestor is added to the watch list automatically.
7.Activate the Business Rule:
Once you're confident that the Business Rule works correctly, activate it so that it starts triggering on relevant records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 09:38 PM
@Community Alums
You can write Before update business rule, below is the script:(Replace xyz with correct field name)
(function executeRule(current, previous) {
// Check if the checkbox field "xyz" is true and the record is being updated
if (current.xyz == true && current.update()) {
var currentUser = gs.getUserID();
// Get the existing watchlist
var watchlist = current.watch_list.toString();
// Check if the current user is not already in the watchlist
if (watchlist.indexOf(currentUser) == -1) {
// Add the current user to the watchlist
watchlist += "," + currentUser;
current.watch_list = watchlist;
current.update();
gs.info("User " + currentUser + " has been added to the watchlist of record " + current.getDisplayValue());
}
}
})(current, previous);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 10:06 PM
HI @Community Alums ,
I trust you are doing great.
You can create on request business rule .Define the Filter condition to specify when this rule should execute. In this case, we want it to trigger when an update is made on the requested item table (sc_req_item
) where the checkbox field xyz
is true. Here's an example of the filter condition:
current.table_name == 'sc_req_item' && current.xyz == true && current.update()
Buisness Rule Script :
(function executeRule(current, previous) {
// Get the user making the update
var userId = gs.getUserID();
// Add the user to the watchlist
current.watch_list = current.watch_list + ',' + userId;
})(current, previous);
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi