- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2025 01:47 PM
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:
AFTER:
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:
Does anyone have a working solution for this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2025 06:08 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2025 02:00 PM - edited ‎02-04-2025 06:11 PM
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");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2025 06:08 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2025 05:15 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2025 09:19 PM - edited ‎02-04-2025 09:19 PM
you told this script is in global scope so you should use this syntax to use ArrayUtil and it should remove duplicates
global.ArrayUtil()
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader