delete members from watchlist on INC/RITM table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2024 05:32 AM
Hi All,
I have 2 business rules that runs on INC/RITM to add watchlist members every time a CI is changed or updated.
The below script queries a rel table and gets the members and adds to the watchlist field.
var user_ids = [];
var grWatchlist = new GlideRecord('u_m2m_from_cmdb_ci_service_watchlist');
grWatchlist.addQuery('u_cmdb_ci_service', current.getValue('business_service'));
grWatchlist.query();
while(grWatchlist.next()){
user_ids.push(grWatchlist.getValue('u_user'));
}
current.watch_list = user_ids.join(',');
Now the requirement is to also delete the members from watchlist field if any of the users are being deleted from the above custom table. I started with the below Business Rule and stuck with the logic.
(function executeRule(current, previous /*null when async*/ ) {
var table = ["incident", "sc_req_item"];
for (var i = 0; i < table.length; i++) {
var gr = new GlideRecord(table[i]);
gr.addQuery('business_service', current.u_cmdb_ci_service);
gr.query();
while (gr.next()) {
//gr.deleteRecord();
}
}
})(current, previous);
Need help with the same.
TIA.
@Tai Vu @Runjay Patel @Uncle Rob @Brad Bowman @Community Alums
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 06:46 AM
Hi @DPrasna ,
Use below code in both BRs.
var gr = new GlideRecord('incident');
gr.addQuery('business_service', current.u_cmdb_ci_service); // replace with actual field name based on table
gr.query();
while (gr.next()) {
gr.watch_list = ''; // Set the watch_list field to empty
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 05:59 AM
@Dr Atul G- LNG @Sandeep Rajput @Abhay Kumar1 Can you please help with the scenario?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 06:37 AM
Hi @DPrasna
Sorry , but I am not in coding, you try the code by @Runjay Patel
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 10:36 AM
@DPrasna Please try below a script it should fit with your requirement:
(function executeRule(current, previous /*null when async*/) {
// Define the tables to update
var tables = ["incident", "sc_req_item"];
// Iterate over each table
for (var i = 0; i < tables.length; i++) {
var gr = new GlideRecord(tables[i]);
gr.addQuery('business_service', current.u_cmdb_ci_service); // Match the business service
gr.query();
while (gr.next()) {
// Get the current watchlist as an array
var watchlist = (gr.watch_list || '').split(',');
// Remove the deleted user from the watchlist
var updatedWatchlist = watchlist.filter(function(user) {
return user !== current.u_user.toString(); // Ensure comparison as strings
});
// Update the watchlist if it changed
if (watchlist.length !== updatedWatchlist.length) {
gr.watch_list = updatedWatchlist.join(',');
gr.update();
}
}
}
})(current, previous);
Logic is perfectly fine and feel free to amend if required.