Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Servicenow unique values from array

servicenow14710
Tera Expert

Hello developers, i have implemented gliderecord to get array. In this array i need a partuclar column as unique values. In short i need to remove duplicate values from array . Any help is appreciated, Thanks!

1 ACCEPTED SOLUTION

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

6 REPLIES 6

Bert_c1
Kilo Patron

Seems you need to provide more details on your goal.

 

Here's a good resource on getting assistance here:

 

tips-on-asking-question-on-community

 

Post your GlideRecord script.

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

Hi @servicenow14710 ,

 

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


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

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