How to display only unique values from the Reference variable

shaik_irfan
Tera Guru

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

1 ACCEPTED SOLUTION

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();
	},

View solution in original post

40 REPLIES 40

I am sure, this is working but i have 4 lakh records from which unique value needs to be searched. The page seems to be loading and loading. Can we write it to perform faster? Perhaps with GlideAggregate or GlideAjax?

that works, thanks man

 

this is working solution 

Modify your script accordingly 

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.push(cc1.sys_id+',');
}

return 'sys_idIN'+ccList;
},
	
    type: 'ScriptIncludeName'
});

This should work 
Mark the response as correct if that really helps 

Thanks
Siva

I dont know what is wrong it still not working 😞