Sagar Pagar
Tera Patron

Hi @servicenow14710,

 

You need to use the ArrayUtil to get unique elements. It removes duplicate elements from an array.

var uniqueElements = new ArrayUtil().unique(arrayName);

 

For reference - ArrayUtil 

 

Thanks,

Sagar Pagar

The world works with ServiceNow

View solution in original post

Not applicable

Hi @servicenow14710 ,

 

Agree with @Sagar Pagar , you need to use ArrayUtils and then use gliderecord over the array.

Maddysunil
Kilo Sage

@servicenow14710 

To remove duplicate values from an array obtained through GlideRecord, you can use JavaScript's Set object.

 

// Create a new Set object to store unique values
var uniqueValues = new Set();

// Iterate through the GlideRecord results
while (gr.next()) {
    // Get the value of the column you want to make unique
    var columnValue = gr.getValue('your_column_name_here');

    // Add the value to the Set (which automatically removes duplicates)
    uniqueValues.add(columnValue);
}

// Convert the Set back to an array
var uniqueArray = Array.from(uniqueValues);

 

  • Set is a JavaScript object that stores unique values. By adding values to a Set, duplicates are automatically removed.
  • Finally, you convert the Set back to an array using Array.from().

This way, uniqueArray will contain unique values extracted from the specified column in your GlideRecord results.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

 

abirakundu23
Giga Sage

Hi @servicenow14710 ,

To find unique elements from an array & remove duplicate elements from an array. you have to use "Array Util'.

var arrayUtil = new ArrayUtil(); // API

Sample code:

var arrayUtil = new ArrayUtil(); //OR 'new global.ArrayUtil(); if from a scoped app;

var array1 = [“INC1###”, “INC2###”, “INC3###”, “INC4###”, “INC5###”];

gs.print(“Contains INC1###: ” + arrayUtil.contains(a1, “INC1##”)); //Contains a specified value 

gs.print(“unique values: ” + arrayUtil.unique(array1)); //Gets unique element.

 var array2 = [“INC1###”, “INC2###”, “INC7###”, “INC4###”, “INC4###”]; /Combines 2 arrays and gets unique values

gs.print(“Taking unique Value: ” + arrayUtil.union(array1, array2));

 

Reference Link: ServiceNow Array Utils
https://youtu.be/5zXfBU1DzUU?si=DNJigsS44wovpK3C


Please mark helpful & accept my answer if it's really worthy for you.

abirakundu23
Giga Sage

Hi @servicenow14710 ,

Please accept the solution if it's solved your purpose.