Need help adding manager to a watchlist using business rules

JMitchell
Kilo Expert

Hello everybody,

I'm working on a business rule script to add a user's manager to a watchlist. I have a script that is working, and it validates the manager isn't already a member of the watchlist. What I'm looking for here, is maybe an idea on how I would dynamically change the manager if the caller changes. Also, if you have an idea on how I can do this better, I'm always open to suggestions. 

(function executeRule(current, previous /*null when async*/) {
// Add your code here

var wl = current.watch_list.toString();
var array = wl.split(",");
var mgr = current.caller_id.manager;

if (current.watch_list == ''){
current.watch_list += mgr;
} else if (array.indexOf(mgr) > -1 && current.watch_list != ''){
current.watch_list += ',' + mgr;
}

})(current, previous);

 

Thank you in advance,

James Mitchell

1 ACCEPTED SOLUTION

This will help

https://community.servicenow.com/community?id=community_blog&sys_id=a84daee5dbd0dbc01dcaf3231f96198e

View solution in original post

7 REPLIES 7

This will help

https://community.servicenow.com/community?id=community_blog&sys_id=a84daee5dbd0dbc01dcaf3231f96198e

I think this is exactly what I need. Thank you!

JMitchell
Kilo Expert

I was wrong earlier. The code I had posted earlier wouldn't add the manager if ANYBODY was in the watch list. I've updated my code and am posting it here in case somebody finds this post later and is looking for ideas:

 

 

(function executeRule(current, previous /*null when async*/) {
// Add your code here

var wla = current.watch_list.toString().split(","); //array containing watch_list members
var mgr = current.caller_id.manager;
var i;
var chk=0; //0= false, 1= true

//Loop through the watchlist to see if the user's manager is already a member

for(i=0;i < wla.length;i++){
if(wla[i] == mgr){
chk = 1;
break;
}
}

//If the watchlist is empty, add the manager.
//If the watchlist is not empty, and does contain the manager as a member, add the manager.
//Display a message indicating who was added to the watchlist


if (current.watch_list ==""){
current.watch_list += mgr;
gs.addInfoMessage(mgr.getDisplayValue() + " has been added to the watchlist");  
}else if (current.watch_list != "" && chk == 0){
current.watch_list += "," + mgr;
gs.addInfoMessage(mgr.getDisplayValue() + " has been added to the watchlist");
}

})(current, previous);