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

If the assignment group field has duplicate names , how are you managing exact assignment groups for all the records. Initially i suggest you to run a background script removing all the duplicate assignment groups 

9

Before that for the existing records you should also check if the assignment group(you are deleting) has already been assigned to a case and maintain a single unique assignment group with the same sysid for every existing case, i strongly suggest that it would be the better approach as it may even incur multiple problems in reporting 

 

We can prepare a code for the request by modifying what you have written but we cannot be sure how GlideRecord takes in when you query the same name for an assignment group what ever first record comes up it gives that sys id and that might become a mess .. 

 

Hope that helps

 

Mark this response as correct if that really helps 

 

Thanks,

Siva

Alikutty A
Tera Sage

Hi Shaik,

You can try this script in your script include

var ccList = [];
var uniqueArr = [];
var arrayUtil = new ArrayUtil();
var cc = new GlideRecord('cmn_cost_center');
cc.query();
while(cc.next()){
  ccList.push(cc.name);
}
uniqueArr = arrayUtil.unique(ccList);
return 'sys_idIN'+uniqueArr.toString();

Add this script with a function named getUniqueCostCenters() and Call it in the advanced reference qualifier of cost center variables as

javascript: new ScriptIncludeName().getUniqueCostCenters()

Few more changes are required to the above script, please try the script below

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);

ccList = [];
var cc1 = new GlideRecord('cmn_cost_center');
cc1.addEncodedQuery('nameIN'+uniqueArr.toString());
cc1.query();
while(cc1.next()){
  ccList.push(cc1.sys_id);
}

return 'sys_idIN'+ccList.toString();

Ali,

 

Thank you for the script but unfortunately it is not working 😞

 

Advanced Rer Qualifier Screnshot:

find_real_file.png

 

Script Include:

 

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);

ccList = [];
var cc1 = new GlideRecord('cmn_cost_center');
cc1.addEncodedQuery('nameIN'+uniqueArr.toString());
cc1.query();
while(cc1.next()){
  ccList.push(cc1.sys_id);
}

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

 

I see only 1 record other records got disapperaed.

find_real_file.png