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

very elegant solution.  Thanks!

Thanks 🙂

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


I was told to use JSUtil.unique() but ServiceNow did not recognise it. It also did not recognise the Set command that gives you unique array.

So basically the ArrayUtil() was the life saver. Thanks you.

laszlobolya
Kilo Expert

thank you guys, really, very helpful all. We go with a bit different approach. I marked Harsh's answer correct, but may the others also



Maybe could you give advise on about arrayUtil.diff



The problem there.


1. Query the custom table u_triplet_matrix we store Categories related to CI, get back Categories for the CI on INC, remove duplicates, add itt to Array, ok. Array name validCatArray. Example: SERVER MGMT, STORE MGMT,


2. push to this Array, the actual, selected category on INC, say it DATABASE MGMT, we get from Client script Ajax, ok. I can see with gs.log I have all the Valid category from from the query + the actual one, so my validCatArray now: SERVER MGMT, STORE MGMT, DATABASE MGMT,


3. We push the actual, because later we dont want to get removed the actual selection when ticket is loaded.


4. Query the sys_choice table, where the name (table) is u_triplet_matrix, element is u_category., get all active items. Store it in a different Array, allCatArray. As example this array has SERVER MGMT, STORE MGMT, DATABASE MGMT, APP MGMT, EXCHANGE MGMT



I want to remove the validCat items from allCat, so here we go:


5. now come to the Diff! return arrayUtil.diff(allCatArray,validCatArray).join(',');



And the return is: DATABASE MGMT, APP MGMT, EXCHANGE MGMT



SERVER MGMT, STORE MGMT was removed, which is great, because on Client I use g_form.removeOption. BUT! Why DATABASE MGMT is on the result? Why the .diff did not take it out? It is a part of validCatArray, also the allCatArray, but not taken out somehow



The value and label is the same, string choices. Thanks if any idea!