Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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.