The CreatorCon Call for Content is officially open! Get started here.

ArrayUtil difference is not working as expected

SHA3
Giga Expert

Hi,

I am trying to find difference of list collector to find the new values entered by user, have tried many ways, below is the current code i have

Note: Also tried using jQuery.control to fetch old value since that didnt work as well declared a hidden field "u_oldusers" to hold the values previously entered by the user to compare with new values and then to get difference of both using Arrayutil.diff(<new>,<old>). But for some reason, difference is passing all values in Newvalues instead of passing only difference.

u_oldusers on alerting does provide previously selected values and i am trying to capture the difference even before newvalues are updated to oldvalues filed.

Onchange Client script:

---------------------------------------------------------------------------------------------------------------------------------------

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading) {

          return;

    }

  var delay = 2000;//2 milliseconds

  if(newValue == ''){

  g_form.setDisplay('u_cs_001',false);

  g_form.setDisplay('u_table',false);

  }else{

  g_form.setDisplay('u_cs_001',true);

  g_form.setDisplay('u_table',true);

  }

  var d = g_form.getValue('u_oldusers');

  var a = []; var b = []; var c = g_form.getValue('u_select_users');

  g_form.setValue('u_newusers',c); var n = [];

//Type appropriate comment here, and begin script below

  //alert('old users: '+g_form.getValue('u_oldusers')+' newusers: '+g_form.getValue('u_select_users'));

  if(n != d)

  {

  a = g_form.getValue('u_oldusers').toString(); a = a.split(',');  

  if (a == null){a = [" "];}

  n=g_form.getValue('u_newusers').toString();n = n.split(',');

  //alert('a '+a+' b '+b);

  var ga = new GlideAjax('CatAjaxUtils');

  ga.addParam('sysparm_name','getdifference');

  ga.addParam('sysparm_old_users',a);

  ga.addParam('sysparm_newusers',n);

  ga.getXMLWait();

    var ans = JSON.parse(ga.getAnswer());

  //alert('ans is '+ans);

   

alert('old users: '+g_form.getValue('u_oldusers')+' -- a-- '+a+' newusers: '+g_form.getValue('u_newusers'));

  angular.element(document.getElementById("newApp")).scope().addData1(ans);

  angular.element(document.getElementById('newApp')).scope().$apply();

  }

  g_form.setValue('u_oldusers',c);

}

-------------------------------------------------------------------------------------------------------------------------------

Script Include Ajax call function:

getdifference: function(){

  var arrayUtil = new ArrayUtil(); var prevusers = []; var newusers = [];

prevusers = this.getParameter('sysparm_old_users').toString();

  if(prevusers != null)

  {prevusers = prevusers.split(',');}

  else{

  prevusers = [];}

newusers = this.getParameter('sysparm_newusers').toString();newusers = newusers.split(',');

  gs.log('prev users: '+ prevusers+ ' newusers '+newusers);

var difference = []; difference= arrayUtil.diff(newusers,prevusers);

gs.log('difference '+ difference);

return JSON.stringify(difference);

},

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Out put of gs.log is like : prev users: [Ljava.lang.String;@ae2b34 newusers [Ljava.lang.String;@17866eb

Appreciate your responses on this.

1 ACCEPTED SOLUTION

Hi Sanjiv,



Thanks for your responses.


I couldn't get a solution using Arrayutil.diff. So i have used a for loop to get to the solution i needed.



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


  {


  if(oldusers.indexOf(newusers[i])== '-1'){


  difference.push(newusers[i]);


}


Difference has the list of users i need. I have added a flag to get the difference between newuser and olduser, so that this line of code runs only when users are added, not otherwise.



List collector, onchange runs even on click of pointer on a value in either side. So adding flag helped me.


View solution in original post

9 REPLIES 9

SanjivMeher
Kilo Patron
Kilo Patron

Hi Sha,



onChange doesnt work for list collectors.



Please mark this response as correct or helpful if it assisted you with your question.

can you please elaborate ? how it doesnt work?



I am asking this cause, i see that list of newusers and old users as well and script include function is being called when there is differnce between newusers and oldusers as expected, only thing is the arrayutil.diff is not providing expected difference, instead it provides all users in newusers list.


Try below. You need to declare two separate variable types. One is array to store the array values and one is string.



getdifference: function(){


var arrayUtil = new ArrayUtil();


var prevusers = ''; var newusers = '';


var prevusersarr = []; var newusersarr = [];


prevusers = this.getParameter('sysparm_old_users').toString();


  if(prevusers != null)


  {prevusersarr = prevusers.split(',');}


  else{


  prevusersarr = [];}


newusers = this.getParameter('sysparm_newusers').toString();


newusersarr = newusers.split(',');


  gs.log('prev users: '+ prevusersarr + ' newusers '+newusersarr );


var difference = []; difference= arrayUtil.diff(newusersarr ,prevusersarr );


gs.log('difference '+ difference);


return JSON.stringify(difference);


},



Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,



Thanks for your help. this is not working as well same result as it was before.