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

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka Chaud1 

you can use simple javascript logic and remove elements from array and then set values again

Remove multiple elements from array in Javascript/jQuery 

How to remove multiple elements from array in JavaScript ? 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I used this as suggested by you but still not working @Ankur Bawiskar can you please check the code
 
(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=new ArrayUtil().unique(new_memb.split(',')).splice();
}


})(current, previous);



@Priyanka Chaud1 

I assume your BR is before update with condition as priority changes

try this

(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'));
    }

    if (current.priority == 1 || current.priority == 2) {
        var new_memb = '';
        if (current.watch_list.toString().length > 0)
            new_memb = current.watch_list.toString() + ',' + members.join();
        current.watch_list = new ArrayUtil().unique(new_memb.split(',')).join();
    } else {
        // logic to remove
        var existingUsers = current.watch_list.toString().split(',');
        var removalIndex = [];

        for (var j = 0; j < existingUsers.length; j++) {
            if (members.indexOf(existingUsers[j]) > -1)
                removalIndex.push(j);
        }

        for (var i = removalIndex.length - 1; i >= 0; i--)
            existingUsers.splice(removeValFromIndex[i], 1);

        current.watch_list = existingUsers.join();
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hey @Ankur Bawiskar tysm for reply but sohail replied first the code and i implemented it was working