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

Hi Chuck,



I'm not sure which util I need - how would just identifying where they interest give the ones that aren't in that intersection? I would assume if I do the intersect between "Bob, Sue" and "Bill, Sue, Jack" would give me "Sue". But then I want to remove Sue from "Bob, Sue" so I'm left with only "Bob." None of the utils seem to say remove anything from array 2 from array 1 and give me the new array 1.


You are correct. ArrayUtil doesn't contain anything for removing elements.



This may help... I originally wrote it for glide_list fields and includes methods for adding and removing elements. You should be able to adapt it pretty easily.



Managing Glide Lists


Hi Chuck,



So, I had been trying to use splice previously, but it kept failing. I've updated my script to this and again, I just keep returning the entire existing work notes list even though some of the IDs are previous assignment group members.



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


  if(previous.assignment_group){


  var pM = new GlideRecord('sys_user_grmember');


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


  pM.query();


  while(pM.next()){


  if(wnList.indexOf(pM.user.sys_id) != -1){


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


  wnList.splice(pos,1);


  }


  }


  gs.info('wnList is now ' + wnList);


  }


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);


Glad you got it addressed Kristen!