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

MG Casey
Mega Sage

When pushing items into the array, you might have better success using "getValue(fieldName)" instead of directly calling the field name. Sometimes what it brings back isn't a string.

 

For what it's worth - I like doing method #2. Also - side note - try to avoid using "gr" as variable names if at all possible.

 

Example:

var myEnv = [];

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

//loop through results of query and add environment record to array
while (findRecordsGR.next()) {
    if (myEnv.indexOf(findRecordsGR.getValue("environment")) === -1)
        myEnv.push(findRecordsGR.getValue("environment"));
}

gs.log("Envs: " + myEnv, "JOE1");

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

YES!!!  That worked!

Thank you so much!

 

I am not sure why none of the other methods removed the duplicates, but I am just happy to find one that did work.

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

you told this script is in global scope so you should use this syntax to use ArrayUtil and it should remove duplicates

global.ArrayUtil()

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 global.ArrayUtil();
    var uniqueEnv = au.unique(myEnv);

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

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

},

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