- 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:57 AM
Mark,
what you recommended it's working but for my requirement isn't working ie. below script trying to remove duplicates. I dont know where the issue is
var company = 'Test';
var name = '230820201029';
var getNewModel;
var newModel = 'Network Gear,UNIX Server';
var modelName = company+' '+name;
var sysIds = new TATAHWModelMatrix();
var availableIds = sysIds.getExistingModel(modelName); // Get existing Model-Model Categories sysid
var newIds = sysIds.getModelSysIds(newModel); // Get new model - model categories sys id
var getNewModel = '"'+availableIds+','+newIds+'"';
gs.log('Existing:'+availableIds);
gs.log('New one:'+newIds);
gs.log('Conclusion:'+getNewModel);
var x = [];
x = getNewModel.split(","); // it's a combination of availableIds and newIds.
var arrayUtil = new ArrayUtil();
var a1 = new Array(x);
gs.print('Interception:'+arrayUtil.unique(a1)); // duplicate is not removed
Defined function in Script Include,
getExistingModel: function(getModelName) {
var getModel = [];
var sysIds;
if(getModelName !=null ||getModelName !=''){
var modelHWName = new GlideRecord('cmdb_hardware_product_model');
modelHWName.addQuery('display_name',getModelName);
modelHWName.query();
if(modelHWName.next()){
sysIds = modelHWName.cmdb_model_category;
if(sysIds!=null || sysIds ==''||sysIds !='undefined')
return sysIds;
}
}
},
getModelSysIds: function(getModelNames) {
var arr = [];
var modelArray = getModelNames.toString().split(",");
var getModelIDs = new GlideRecord('cmdb_model_category');
getModelIDs.addQuery('name', 'IN', modelArray);
getModelIDs.query();
while (getModelIDs.next()) {
var ids = getModelIDs.sys_id;
arr.push(ids.toString());
}
return arr.join();
},
Suresh.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2020 03:59 AM
Can you explain what you are trying to achieve? Where it is going wrong? I see you've added logs, what do these tell you?
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 04:07 AM
Each model having model categories. I've taken sample model which is having a model category and try to add new model categories through background script. I'm trying to do the check the new and existing model categories are having the same values. if it's in removing that duplicated ones.
getExistingModel - Finding the model category for that model name.
getModelSysIds - changeing the sysid for newModel variable.
Regards,
Suresh
Suresh.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2020 04:11 AM
So your issue is how you build getNewModel? Because you are building this as one big string?
Have you considered something like:
var modelArr = [];
modelArr.push(availableIds) or modelArr.push(newIds) or whereever your id is comming from originally.
This way you will actually build an array instead of just one big string with:
var getNewModel = '"'+availableIds+','+newIds+'"';
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
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 04:19 AM
If you want to check if it's already in the existing array, it's possible to use ArrayUtil.contains() method in your loop instead of building an array and then removing duplicates.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list