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 have something similar:



var haveCategory = [];


var grSAA = new GlideRecord('x_fstfs_service_ac_service_acceptance_task');


grSAA.addQuery('service_acceptance', current.sys_id);


grSAA.addQuery('requirement.category', 'IN' , current.u_requirements);


grSAA.query();


while (grSAA.next()){


haveCategory.push(grSAA.requirement.category);


  }  


}



haveCategory is an object with a list of sys ids


current.u_requirements is an object with a list of sys ids



I want to retrieve the sys ids of the items in current.u_requirements which ARE NOT in haveCategory.



I have tried to use the ArrayUtil().diff(), but had no luck. Please could you suggest. Thanks


You probably need to do something similar to what I had to do: when pushing the category, be sure to push .sys_id.toString() and if your other list is built from a similar query, then do the same thing.


You might want to take a look at this script include I posted a while back to help manage the lists and make the code a bit more readable. You'll need to go through the source list/array element by element to see if that particular sys_id is "IN" the list. There's a method in the script include that does that.



Managing Glide Lists