Eliminating Duplicates Records from an Array in a Script Includes

jmiskey
Kilo Sage

I have a function in a Script Includes that I am using as criteria for a Reference field in a Catalog Item.  I will also mention that this is a Scoped Application, in case that makes any difference.

 

The Script Includes is creating an array that I used in my Reference field selection criteria.  The issue is, that there are duplicate sys_ids in the array between returned.  So I looked at many articles and past questions to see if I could figure out how to eliminate the duplicates.  I found 3 methods, but none of them work (all 3 are in the code, 2 are currently commented out)!  If I check the values in the array before and after I run the step to eliminate the duplicates, they return the exact same records, and I confirmed there are duplicates in the lists.

 

Here is the code of my Script Includes, and I show all three methods I have tried:

    getEnvironmentFilter:function(){
        //get key variables from request form
        var proj = current.variables.project_name;

		//instatiate array variable
		var myEnv = [];

		//glide record on Workspace Access table torecords matching project name (and are active and requestable)
		var gr = new GlideRecord('x_ebcbs_databricks_workspace_access');
		gr.addQuery('active','true');
		gr.addQuery('requestable','true');
		gr.addQuery('project',proj);
		gr.query();

		//loop through results of query and add environment record to array
		while(gr.next()){
			myEnv.push(gr.environment.sys_id);
		}
		gs.log('Envs: ' + myEnv,"JOE1");

	//**METHOD 1 **/
		// //remove duplicates from array
		// var uniqueEnv = myEnv.filter(function(item, index) {
		// 	return myEnv.indexOf(item) === index;
		// });

	//**METHOD 2 **/
		// var uniqueEnv = [];
		// for(var i = 0; i < myEnv.length; i++){
		// 	//check to see if value array already; if not, add it
		// 	if(uniqueEnv.indexOf(myEnv[i]) === -1){
		// 		uniqueEnv.push(myEnv[i]);
		// 	}
		// }

	//**METHOD 3 **/
		//remove duplicates from array
		var au = new ArrayUtil();
		var uniqueEnv = au.unique(myEnv);

		gs.log('Unique ENVs:' + uniqueEnv,"JOE2");

		//return query
		var crit = "sys_idIN" + uniqueEnv.join();
		return crit;

    },

 

Here are the results of the Logs, where I highlighted just one set of the duplicates (but there are others too):

 

BEFORE:

jmiskey_0-1738705454021.png

 

AFTER:

jmiskey_1-1738705469305.png

 

And I get the exact same three results no matter which of the 3 methods of removing duplicates that I try.

Why isn't this working?  Is it something to do with this being in a Script Include or Scoped Application?

BTW, in case anyone wants to understand more of what I am trying to do, it is related to this question: 

 

https://www.servicenow.com/community/developer-forum/tricky-reference-qualifier-on-catalog-item-vari...

 

Does anyone have a working solution for this?

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

Try checking for a duplicate before adding to the array, like:

		while(gr.next()){
			if (!myEnv.includes(gr.environment.sys_id)) {
				myEnv.push(gr.environment.sys_id);
			}
		}

the methods are not needed

View solution in original post

7 REPLIES 7

Unfortunately, those changes made no difference.  It is still returning duplicates.

Note that the Catalog Item and Data Tables are in a Scoped Application, but the Script Includes is in the Global Application (though the Script Includes is marked as being Accessible in all application scopes).

 

Just for kicks, I tried moving the Script Includes to the Scoped App, but then it did not work at all.

@jmiskey 

Changes I mentioned should work fine i.e. using global.ArrayUtil() from your scoped app

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

As I said, unfortunately, that did not work for me.

Since I got a working solution using a different method, I am done with this issue, and moving on to the next one.