- 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
‎07-23-2017 09:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2017 10:01 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2017 11:09 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2017 12:52 PM
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.