Remove member from watchlist

Priyanka Chaud1
Tera Contributor

Hi folks,

I had a requirement to add users to watchlist when priority is High/Critical I created br to get members from group and its working fine. But issue is when priority of same incident changes to low again the members are not removed from the watchlist. So they are still notified. Attaching br please help me how to remove users when priority is not high/critical @Ankur Bawiskar 

(function executeRule(current, previous /*null when async*/) {


  var members=[];

  var gr= new GlideRecord('sys_user_grmember');


  gr.addQuery('group.name','Incident Notification Alert - P1 Group Members'); 


  gr.addQuery('user.active',true);


  gr.query();


  while(gr.next()){


  members.push(gr.getValue('user'));



  }


  var new_memb='';


  if(current.watch_list.toString().length>0)


  new_memb=current.watch_list.toString()+','+members.join();


  else


  new_memb=members.join();


  current.watch_list=new ArrayUtil().unique(new_memb.split(',')).join();
 

 


})(current, previous);



1 ACCEPTED SOLUTION

sohail_mohamad
Tera Expert

Try this

 

(function executeRule(current, previous /*null when async*/) {
    var members = [];
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group.name', 'database'); // give your group here
    gr.addQuery('user.active', true);
    gr.query();

    while (gr.next()) {
        members.push(gr.getValue('user'));
    }

    var currentWatchList = current.watch_list ? current.watch_list.split(',') : [];
    var newWatchList = [];

    if (current.priority < 3) { // Priority 1 or 2
        newWatchList = currentWatchList.slice();
        var membersToAdd = members.filter(function(member) {
            return newWatchList.indexOf(member) === -1;
        });

        newWatchList = newWatchList.concat(membersToAdd);
    } else {
        newWatchList = currentWatchList.filter(function(userId) {
            return members.indexOf(userId) === -1;
        });
    }

    current.watch_list = new ArrayUtil().unique(newWatchList).join(',');

})(current, previous);
 
 

View solution in original post

13 REPLIES 13

Mark Manders
Mega Patron

This is the BR for adding users. What have you created to remove them? A business rule does exactly what you say it should do and when. It's not automatically reversed if the conditions aren't met anymore.

 

So you need a new BR or flow with conditions that it changes from high to low (or whatever requirements you have) and remove the members from the watchlist. The question is: are there any other people on the watchlist, or is it just the group? If it's just the group, you can just set watchlist to empty. If others could be in there, you will just need to remove them from the watchlist, so the others remain.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

When priority will be low/moderate watchlist will be empty can u check and modify my code for that it would be great help thanks @Mark Manders 

I don't believe your the adjustment to your script is a good thing to do. I added the 'else, empty watchlist' below, but really look at your process. Do you never, on no ticket have anyone on the watchlist, except on P1/P2? 

Because your script runs on every Priority change (I think) and will empty it also if it goes from 3 to 4. You really should consider making it 2 rules, one adding the groupmembers, and the other one removing them.

(function executeRule(current, previous /*null when async*/) {

    var members=[];
    
    if(current.priority ==1 || current.priority==2){
        var gr= new GlideRecord('sys_user_grmember');  
        gr.addQuery('group.name','Incident Notification Alert - P1 Group Members'); //put your group name here
        gr.addQuery('user.active',true);
        gr.query();
        while(gr.next()){
        members.push(gr.getValue('user'));
        }
    
    var new_memb='';
    if(current.watch_list.toString().length>0){
    new_memb=current.watch_list.toString()+','+members.join();
    }else{
        new_memb=members.join();
    }
    current.watch_list=new ArrayUtil().unique(new_memb.split(',')).join();
    }else{
   current.watch_list='';
   }
})(current, previous);

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Your solution not working @Mark Manders please let me know changes in same br if u can

What is isn't working? I just ran it on my PDI and it was emptying the watchlist when priority changed to anything but 1 and 2. The rest I took from your script which was working according to your question.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark