- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2019 01:38 AM
Hello,
We have a variable Reference Type which is referring to Assignment Group. Our Assignment Group contains duplicate group names ex: Test Group, Test Group, Network, Network
From the Catalog view when the user clicks or search for the Assingment Group we need to display only unique value like if the user types Network, it should display only Network once not multiple
How to acheive this
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2019 11:02 PM
Last one may not work, This was tougher than I thought, I would really suggest you control the duplicate names at the table level for cost center, This is really not a recommended approach.
I have tested this script and it works, Please try this out
getUniqueCostCenters: function(){
var ccList = [];
var uniqueArr = [];
var ids = [];
var arrayUtil = new ArrayUtil();
var cc = new GlideRecord('cmn_cost_center');
cc.query();
while(cc.next()){
ccList.push(cc.name.toString());
}
uniqueArr = arrayUtil.unique(ccList);
ccList = [];
for(var i=0; i<uniqueArr.length; i++){
var cc1 = new GlideRecord('cmn_cost_center');
cc1.addEncodedQuery('nameIN'+uniqueArr[i]);
cc1.query();
if(cc1.next()){
ccList.push(cc1.sys_id.toString());
}
}
return 'sys_idIN'+ccList.toString();
},

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2019 11:11 PM
Hi
Why don't you use glide aggregate and do groupby ? Please check the below :
https://docs.servicenow.com/bundle/jakarta-application-development/page/script/glide-server-apis/concept/c_GlideAggregate.html
Regards.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2019 11:13 PM
Yes you can use glideAggregate as well as that may be as less customized ... But not sure of the internal performance specifics if it really makes an impact

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2019 11:11 PM
var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUniqueCostCenters: function(){
var ccList = [];
var uniqueArr = [];
var ids = [];
var arrayUtil = new ArrayUtil();
var cc = new GlideRecord('cmn_cost_center');
cc.query();
while(cc.next()){
ccList.push(cc.name);
}
uniqueArr = arrayUtil.unique(ccList);
uniqueArrFinal = uniqueArr.join();
ccList = "";
var cc1 = new GlideRecord('cmn_cost_center');
cc1.addEncodedQuery('nameIN'+uniqueArrFinal);
cc1.query();
while(cc1.next()){
ccList=cc1.sys_id+',';
}
return 'sys_idIN'+ccList;
},
type: 'ScriptIncludeName'
});
This should work now please try it and lemme know
Mark the response as correct if that really helps
Thanks
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2019 11:13 PM
Please try the last script I posted, it was tested as working

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2019 11:25 PM
Hello Shaik,
I just tested it in BG script and Yes, the last code posted by Ali is working
var ccList = [];
var uniqueArr = [];
var ids = [];
var arrayUtil = new ArrayUtil();
var cc = new GlideRecord('cmn_cost_center');
cc.query();
while(cc.next()){
ccList.push(cc.name.toString()); //toString made the magic here
}
uniqueArr = arrayUtil.unique(ccList);
gs.print(uniqueArr);
ccList = [];
for(var i=0; i<uniqueArr.length; i++){
var cc1 = new GlideRecord('cmn_cost_center');
cc1.addEncodedQuery('nameIN'+uniqueArr[i]);
cc1.query();
if(cc1.next()){
ccList.push(cc1.sys_id.toString());
}
}
gs.print(ccList);