Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to add new watchlist group without losing existing users via Business Rule

skrishna
Kilo Contributor

    Hi,

We have a request to add a group members to watchlist when it assign to a specific group. Able to fetch this data. But this is overriding the existing users of watchlist instead of appending to it.

Table: incident

When: Before

Insert:True

Update: true

Condition: Assignment group changes AND Assignment group contains "XYZ"

script:

var users=current.watch_list;

var gr = new GlideRecord('sys_user_grmember');

gr.addQuery('group','sys_id');

gr.query();

while(gr.next())

  {

              var user =(','+ gr.user);

              current.watch_list += user;

      }

1 ACCEPTED SOLUTION

Michael Fry1
Kilo Patron

Your script looks fine and should work without issues. I tested same script and it appends users to watch list. Maybe there is conflict with another script? Use Field Watcher to check for other scripts running.


Script I tested:


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group',current.assignment_group);


gr.query();


while(gr.next())


  {


              var user =(','+ gr.user);


              current.watch_list += user;


      }


View solution in original post

3 REPLIES 3

Mike Patel
Tera Sage

I'm using below to do that.



for (var i = 0; i < rarray.length; i++) {


  var recipient = rarray[i];


  //Check to see if it's a user address


  var gr = new GlideRecord('sys_user');


  gr.addQuery('email', recipient);


  gr.query();


  if (gr.next()) {


  // It's a user


  if (wList != "") {


  wList = (wList + "," + gr.sys_id);


  } else {


  wList = gr.sys_id;


  }


  } else {


  //It's not a user either...so just add the address to the list...except instance email address


  if (!new ArrayUtil().contains(ignoredEmailArray, recipient)) {


  if (wList != "") {


  wList = (wList + "," + recipient);


  } else {


  wList = recipient;


  }


  }


  }


}


Michael Fry1
Kilo Patron

Your script looks fine and should work without issues. I tested same script and it appends users to watch list. Maybe there is conflict with another script? Use Field Watcher to check for other scripts running.


Script I tested:


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group',current.assignment_group);


gr.query();


while(gr.next())


  {


              var user =(','+ gr.user);


              current.watch_list += user;


      }


Thanks for confirming on script... I had a chance to recheck business rule. My bad, there was Action set mistakenly and it was not cleared.


Script is fine. Now able to append group users to existing watch-list.



Cheers!



Krishna