- 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 11:28 PM
Hi @DB1
You can use the following logic for Insert in After - Insert BR.
(function executeRule(current, previous /*null when async*/) {
var cuser = current.u_user.sys_id + '';
var ci = current.u_business_service.sys_id + ''; // Please change u_business_service field name as per your configuration in u_watch_list table
var incGr = new GlideRecord("incident");
incGr.addEncodedQuery("watch_listLIKE" + cuser + '^cmdb_ci=' + ci);
incGr.query();
while(incGr._next()){
var wl = incGr.getValue("watch_list") + '';
if(wl.indexOf(cuser) == -1){
wl = wl + "," + cuser;
}
incGr.setValue("watch_list", wl);
incGr.update();
}
})(current, previous);
You can use the following logic for Update in After - Update BR.
(function executeRule(current, previous /*null when async*/) {
var cuser = current.u_user.sys_id + '';
var puser = current.u_user.sys_id + '';
if(puser == cuser){
return;
}
var ci = current.u_business_service.sys_id + ''; // Please change u_business_service field name as per your configuration in u_watch_list table
var incGr = new GlideRecord("incident");
incGr.addEncodedQuery("watch_listLIKE" + cuser + '^cmdb_ci=' + ci);
incGr.query();
while(incGr._next()){
var wl = incGr.getValue("watch_list") + '';
wl = wl.replace(puser, cuser);
incGr.setValue("watch_list", wl);
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-04-2023 12:05 AM
Thanks again!! I tried the below code
After - Insert
It adds the new User if I try to add/ Insert new record. However it does not bring up or push the existing Users
(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") + '';
if(wl.indexOf(cuser) == -1){
wl = wl + "," + cuser;
}
incGr.setValue("watch_list", wl);
incGr.update();
}
})(current, previous);
- 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 08:32 PM - edited 12-03-2023 10:35 PM
Hey @DB1
Let's try the below script for your on delete business rule.
(function executeRule(current, previous /*null when async*/ ) {
var userID = current.getValue('u_user');
var grIncident = new GlideRecord('incident');
grIncident.addQuery('watch_list', 'LIKE', userID); //replace your User field name
grIncident.addQuery('cmdb_ci', current.getValue('u_ci'); //replace your CI field name
grIncident.addActiveQuery();
grIncident.query();
while (grIncident.next()) {
var arrUsers = grIncident.watch_list.split(',');
arrUsers.splice(arrUsers.indexOf(userID));
grIncident.setValue('watch_list', arrUsers.join(','));
grIncident.update();
}
})(current, previous);
Seems this is related to an own post.
Need help with Business Rule to insert members to watchlist on Incident
Please consider to mark our comments as solution if it can make your life easier. 😉
Cheers,
Tai Vu