- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 06:23 AM
Hello All,
I have the following requirement:
1. There is a custom table (Related List) on Business Service table table name - u_watch_list
2. The same user could be part of any Incident
3. If that user is removed from the related list or a record is deleted from u_watch_list table then it should check all the Incidents if the user exist as a watchlist member. If yes, them it should remove them from the watchlist.
Need help with achieve the same.
TIA
@Timi @Dr Atul G- LNG @Danish Bhairag2 @Ankur Bawiskar
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 12:31 AM
You mean existing watch list users are getting wiped out and only the newly inserted user is only present? If yes, Try the below code.
(function executeRule(current, previous /*null when async*/) {
var cuser = current.u_user.sys_id + '';
var ci = current.u_cmdb_ci_service.sys_id + ''; //
var incGr = new GlideRecord("incident");
incGr.addEncodedQuery('business_service=' + ci);
incGr.query();
while(incGr._next()){
var wl = incGr.getValue("watch_list") + '';
var fwl = wl.split(',');
var arrayUtil = new ArrayUtil();
if(arrayUtil.contains(fwl, cuser) == false){
fwl.push(cuser);
}
incGr.setValue("watch_list", fwl.join(','));
incGr.update();
}
})(current, previous);
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 07:07 PM
it should be an easy script. Do you want to search only those Incidents where this CI is present?
BR: Before delete on u_watch_list
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 10:31 PM
Hi Ankur, Yes we need to search only the respective CI related Incidents and for the watchlist members present
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 07:36 PM
Hi @DB1
You can use before Delete BR like the one below.
(function executeRule(current, previous /*null when async*/) {
var cuser = current.u_user.sys_id + '';
var incGr = new GlideRecord("incident");
incGr.addEncodedQuery("watch_listLIKE" + cuser);
incGr.query();
while(incGr._next()){
var fwl = [];
var wl = incGr.getValue("watch_list") + '';
wl = wl.split(',');
for(key in wl){
if(wl[key] != cuser){
fwl.push(wl[key]);
}
}
incGr.setValue("watch_list", fwl.join(','));
incGr.update();
}
})(current, previous);
You can also add the CI Condition in the encoded query if you want to remove the user from watch list only for the incidents where the CI is referred. Let me know the field name if you want encoded query for that.
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 11:05 PM
Hi Anvesh, Thanks for the reply I tried and it works as expected for deletion. Can I use the same BR for Update as well meaning when addition happens it has to update the CI because with the current logic works for deletion if watchlist user already present in Incident if not it does not delete and there is no current logic that updates Incident on Insertion. So can I apply befor Insert/ Delete?