- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2020 02:57 AM
Hi,
I'm trying to remove duplicates using the arrayUtil function using the below code. But it's not helped to resolve. Can you help me to understand where the issue is? The expected result is to remove the duplicate in x instead it's printing what present in x.
var x= [];
x = "82d773951bbe5890454e36d5464bcbaa,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,b1ff7854c3031000b959fd251eba8ff5";
var arrayUtil = new ArrayUtil();
var a1 = new Array(x);
gs.print(arrayUtil.unique(a1));
Regards,
Suresh
Suresh.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2020 04:16 AM
Hi Suresh,
few observations
1) you have not declared the array properly
please try this
var x = "82d773951bbe5890454e36d5464bcbaa,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,b1ff7854c3031000b959fd251eba8ff5";
var arr = [];
arr = x.toString().split(',');
gs.info(arr.length);
var arrayUtil = new ArrayUtil();
gs.info(arrayUtil.unique(arr));
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2020 03:09 AM
Hi there,
In your example, x is just one large string. That won't work.
This works fine for me:
var x = ["82d773951bbe5890454e36d5464bcbaa","c4d4fe4937101000deeabfc8bcbe5d91","8ffdf454c3031000b959fd251eba8f1c","c4d4fe4937101000deeabfc8bcbe5d91","8ffdf454c3031000b959fd251eba8f1c","b1ff7854c3031000b959fd251eba8ff5"];
var uniqueArray = new ArrayUtil().unique(x);
gs.info(uniqueArray);
Result:
*** Script: 82d773951bbe5890454e36d5464bcbaa,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,b1ff7854c3031000b959fd251eba8ff5
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2020 03:17 AM
Hello Suresh,
It not working because ,each array elements should be available in '' single/double quotation marks.
Thanks,
Abhishek Gardade
Abhishek Gardade

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2020 03:32 AM
If in your actual script, value of x is from some other operation, you need to convert the String to an array. That is, do .split(",") to convert the string to an array.
var x= [];
x = "82d773951bbe5890454e36d5464bcbaa,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,b1ff7854c3031000b959fd251eba8ff5".split(",");
var arrayUtil = new ArrayUtil();
var a1 = new Array(x);
gs.print(arrayUtil.unique(a1));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2020 03:44 AM
Any luck on this? Or still help needed?
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list