- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2017 09:26 AM
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:
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:
why is that? I need the QUERY also! Where is it? Why only DESKTOP MGMT? Could you give me some hint? thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2017 01:42 PM
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]);
}
Please find the blog below. Thanks to sabell2012 for this awesome blog
ServiceNow Admin 101: You Too Can Do DISTINCT Queries Using GlideRecord
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2019 11:40 AM
very elegant solution. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2019 10:56 PM
Thanks 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2017 01:42 PM
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]);
}
Please find the blog below. Thanks to sabell2012 for this awesome blog
ServiceNow Admin 101: You Too Can Do DISTINCT Queries Using GlideRecord
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2023 03:58 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2017 08:39 AM
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!