Using ArrayUtil to compare and remove overlap

kristenankeny
Tera Guru

I've been struggling with a solution to a business need to add/remove group members from the work notes list, if the group is flagged to be copied on all notifications. I have the add working, but the remove I cannot get to work. After attempting to use splice (it wouldn't work), I decided to try ArrayUtils, but I'm at a loss as to why it's not working. I've tried multiple versions of the script, but these are the most recent:

if(current.assignment_group != previous.assignment_group){

var workNotesList = [];

workNotesList = current.work_notes_list.split(',');

if(previous.assignment_group){

var oldGroupMembers = [];

var p = new GlideRecord('sys_user_grmember');

p.addQuery('group',previous.assignment_group);

p.query();

while(p.next()){

oldGroupMembers.push(p.user.sys_id);

}

gs.info('EEEEE old group members is ' + oldGroupMembers + ' and work notes list is ' + workNotesList);

var arrayUtil = new ArrayUtil();

gs.info('EEEEE difference between lists is ' + arrayUtil.diff(oldGroupMembers,workNotesList));

}

}

When I do it this way, I get the following logs:

EEEEE old group members is 26029f4a37d8de00c6a3a9c2b3990efc,a3744eca3718de00b69a2863b3990eac,b71b6bd6379cde00c6a3a9c2b3990e9a,76ad01f837ad5600c6a3a9c2b3990e3f,9aed05f837ad5600c6a3a9c2b3990eeb,158fce4e3718de00b69a2863b3990e50,ee4e0df837ad5600c6a3a9c2b3990eb1 and work notes list is a3744eca3718de00b69a2863b3990eac,7a7d0db837ad5600c6a3a9c2b3990e01

and:

EEEEE difference between lists is 26029f4a37d8de00c6a3a9c2b3990efc,a3744eca3718de00b69a2863b3990eac,b71b6bd6379cde00c6a3a9c2b3990e9a,76ad01f837ad5600c6a3a9c2b3990e3f,9aed05f837ad5600c6a3a9c2b3990eeb,158fce4e3718de00b69a2863b3990e50,ee4e0df837ad5600c6a3a9c2b3990eb1

The other script is:

if(current.assignment_group != previous.assignment_group){

var workNotesList = [];

workNotesList = current.work_notes_list.split(',');

if(previous.assignment_group){

var oldGroupMembers = [];

var p = new GlideRecord('sys_user_grmember');

p.addQuery('group',previous.assignment_group);

p.query();

while(p.next()){

oldGroupMembers.push(p.user.sys_id);

}

gs.info('EEEEE old group members is ' + oldGroupMembers + ' and work notes list is ' + workNotesList);

var arrayUtil = new ArrayUtil();

gs.info('EEEEE difference between lists is ' + arrayUtil.diff(workNotesList,oldGroupMembers));

}

}

I get the same initial log (since nothing above that changed), but the second log is :

EEEEE difference between lists is a3744eca3718de00b69a2863b3990eac,7a7d0db837ad5600c6a3a9c2b3990e01

You can see that the overlap between these two lists is a3744eca3718de00b69a2863b3990eac. I want to remove this value from my work notes list. How do I do this?

1 ACCEPTED SOLUTION

Hi Chuck - thanks for the pointers, I finally identified the right script and that I needed to set the user's sys_id to string for the splice to work:



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



  if(current.assignment_group != previous.assignment_group){



  var wnList = current.work_notes_list.split(',');



  //remove previous group from work notes list


  if(previous.assignment_group.u_copy_on_notifications == true){


  var pM = new GlideRecord('sys_user_grmember');


  pM.addQuery('group',previous.assignment_group);


  pM.query();


  while(pM.next()){


  var aU = new ArrayUtil();


  var pos = wnList.indexOf(pM.user.sys_id.toString());


  if(pos != -1){


  wnList.splice(pos,1);


  }


  }


  current.work_notes_list = wnList.toString();


  }



  //add new group to work notes list


  var c = new GlideRecord('sys_user_group');


  c.addQuery('sys_id',current.assignment_group);


  c.query();


  if(c.next()){


  if(c.u_copy_on_notifications == true){


  var cu = new GlideRecord('sys_user_grmember');


  cu.addQuery('group',c.sys_id);


  cu.query();


  while(cu.next()){


  current.work_notes_list += ',' + cu.user.sys_id;


  }


  }


  }


  }



})(current, previous);


View solution in original post

12 REPLIES 12

Chuck Tomasi
Tera Patron

Two things.



FIrst, instead of pushing p.user.sys_id, push p.getValue('user');



Second, if you put everything in one array, you can use arrayUtil.unique() to get all the duplicates out. Is that what you're after?


Hi Chuck,



I want to remove anyone in the previous.assignment_group from the current.work_notes_list. So, if my current work notes list includes Bob and Sue, and the previous assignment group are Bill, Sue, Jack, then my new work notes list just just be Bob.



Thanks!


So you're looking for the intersection of two arrays? That looks like a use for



arrayUtil.intersect(array1, array2);



See the comments in the ArrayUtil script include.


https://YOURINSTANCE.service-now.com/nav_to.do?uri=sys_script_include.do?sys_id=fb32a2d8c0a80a6000e9...



Change YOURINSTANCE to your instance name.