ArrayUtil().unique(array); - looking for help

laszlobolya
Kilo Expert

Dear community,

Please give me a hint what I'm doing wrong. what I want to achieve is simple, remove not unique value from a script.

I have a glidequery in script include, then the following to send answer:

matx.query();

var array = [];

while(matx.next()) {

var object = {}; //Initialize a java object that we will return as a JSON

object.category = matx.getDisplayValue('u_category');

array.push(object);

}

var uniqueArray = new ArrayUtil().unique(array);

var json = new JSON();

var data = json.encode(array);

return data;

}

whith this an onload script gives me this:

INCnumber.JPG

So what I need to get back DESKTOP MANAGEMENT once, and QUERY, those are the unique categories

If i modify the code, put everything within one pair of {} like this:

var array = [];

while(matx.next()) {

var object = {}; //Initialize a java object that we will return as a JSON

object.category = matx.getDisplayValue('u_category');

array.push(object);

var uniqueArray = new ArrayUtil().unique(array);

var json = new JSON();

var data = json.encode(array);

return data;

}

the result onLoad is:

INCnumber2.JPG

why is that? I need the QUERY also! Where is it? Why only DESKTOP MGMT? Could you give me some hint? thanks!

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

Hi Laszlo,



var check= new GlideRecord('incident');  


check.addQuery('active', true);  


check.query();      


gs.print('Count is :'+check.getRowCount());  


var arr= [];      


while(check.next()) {        


      var key = check.category.trim();        


      arr.push(key);  


}  


var arrayUtil = new ArrayUtil();  


arr= arrayUtil.unique(arr);  


  //gs.print(arr.length);  


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


      gs.print(arr[i]);  


}




find_real_file.png



find_real_file.png





Please find the blog below. Thanks to sabell2012 for this awesome blog



ServiceNow Admin 101: You Too Can Do DISTINCT Queries Using GlideRecord


View solution in original post

9 REPLIES 9

Harsh Vardhan
Giga Patron

Hi Laszlo,



Please check the below thread. hope it will help you



ArrayUtil unique odd behavior


antin_s
ServiceNow Employee
ServiceNow Employee

Hi Laszlo,



You may try to avoid populating duplicate values into the Array itself. It will give you better performance also. Please find the below code and try.



matx.query();  


 


var array = [];  


 


while(matx.next()) {  




var object = {}; //Initialize a java object that we will return as a JSON  


object.category = matx.getDisplayValue('u_category')+'';  


if(!containsObject(object, array))


array.push(object);  


}




//var uniqueArray = new ArrayUtil().unique(array);  


var json = new JSON();  


var data = json.encode(array);  




function containsObject(obj, list) {


      var i;


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


              if (list[i].category === obj.category) {


                      return true;


              }


      }




      return false;


}



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


Shishir Srivast
Mega Sage

Please check if this helps.



matx.query();            


var array = [];  


var allapps_val= [];


while(matx.next()){


array.push(matx.getDisplayValue('u_category')); }


var allapps_array = array.split(',');


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


if(!allapps_val.contains(allapps_array[i]))


allapps_val.push(allapps_array[i]);


}


return allapps_val;



chirag_bagdai
ServiceNow Employee
ServiceNow Employee

Hi laszlobolya



I think you should try with GlideAggregate because it will give you unique data only. Check out below example where I am getting unique category and preparing the JSON object in the same format which you want.



var count = new GlideAggregate('incident');


count.addQuery('active', 'true');


count.groupBy('category');


count.query();    


var temp_ary = [];


while (count.next()) {


  temp_ary.push({"category" : count.getDisplayValue('category')});


}


gs.log(JSON.stringify(temp_ary));



Let me know in-case of any question.