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

Variable name is Center & Cost Center table field name is name.

 

Do i need to change in Scirpt Include & Ref Qualifier or only Ref Qualifier ?

 

Variable Name:

find_real_file.png

 

Cost Center whcih is a Referring table:

 

find_real_file.png

 

Advanced Script:

find_real_file.png

 

Script Include: I tried keeping as Center too but it is not working in the scriptinclude function and unique field script line

 

find_real_file.png

 

 

 

I missed this in the code Irfan

----------------------------------------------

var arrayUtil = new ArrayUtil();

var store = arrayUtil.unique(duplicateFieldName);

return 'sys_idIN'+store.toString();

--------------------------------------------

 

Mark the answer as correct and helpful if it helped you..!!!

Thank you, Now i dont see any record all gets disapperead 😞

 

find_real_file.png

 

 

Script Include:

var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = {
scriptIncludeFunctionName: function(duplicateFieldName) {
var arrayUtil = new ArrayUtil();

var store = arrayUtil.unique(duplicateFieldName);

return 'sys_idIN'+store.toString();


},

type: 'ScriptIncludeName'
};

 

siva_
Giga Guru

Hello Irfan ,

 

The way you called the script include in the reference qualifier is correct but the thing which you are missing is 

after doing the processing that needs to be done in script include you are getting the items into an array but you are not filtering the duplicates as expected (i mean you are directly returning the array with the names from your script include and that doesnot work) 

 

To make it work , you have to get the sys ids as an comma seperated list of values as you are returning the values to a reference field , it can only display records based on the set of sysid's 

so into your array you can get set of sys_id values and you can use Array.join() after the ArrayUtil.unique() line 

and assign those values to a variable and while returning you can use return "sys_idIN"+varName

 

Hope that helps 

 

Thanks,

Siva

MARK THIS RESPONSE AS CORRECT IF THAT REALLY HELPS

In above response am i missing anything ?