Script to add a specific Group to watchlist based on a checkbox value being changed to TRUE

carlh
Kilo Guru

Hi Everyone,

I would like to add in a check box on my incident form to notify a Specific Group.

Pseudo Code -

Where: Incident

When:   Insert and Update

If "Notify_Management" changes to True, add "Manager" group to the watch list.

Could someone share a script for this based on the info above?

Thank you

Carl

1 ACCEPTED SOLUTION

carlh



Copy the following script as is in the Business rule. I have tested this and it works. Just replace the highlighted portion with your group name



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


  var members=[];


  var gr= new GlideRecord('sys_user_grmember');


  gr.addQuery('group.name','Your group name'); //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();


})(current, previous);


View solution in original post

26 REPLIES 26

Copy this code in there



var members=[];


var gr= new GlideRecord('sys_user_grmember');


gr.addQuery('group.name','your group goes here');


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


gr.query();


while(gr.next()){


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


}


var new_memb='';


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


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


else


new_memb=members.join();


current.watchlist=new_memb;


Tried that too. Were you thinking Before Business rule?



Here's what I tried.






I mean, you need to have that function declarations like executeRule() and put the code inside the function declarations


Still nothing.



Here's what I used



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


var members=[];


var gr= new GlideRecord('sys_user_grmember');


gr.addQuery('group.name','your group goes here');


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


gr.query();


while(gr.next()){


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


}


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


current.watchlist=new_memb;


})(current, previous);


Use this in the BR. Put your group name in the highlighted portion



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


var members=[];


var gr= new GlideRecord('sys_user_grmember');


gr.addQuery('group.name','your group goes here'); //put your group name here


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


gr.query();


while(gr.next()){


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


}


var new_memb='';


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


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


else


new_memb=members.join();


current.watchlist=new_memb;


})(current, previous);