- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 05:55 AM
I have the below script :
Compare two arrays 'arrLaIpAddress' and 'arrCmdbIpAddress', if 'arrLaIpAddress' is not present in 'arrCmdbIpAddress', get those IP address in 'arrIpAddress'.
var ipAddress;
var arrLaIpAddress = [];
var arrCmdbIpAddress = [];
var arrIpAddress =[];
var grDiscoveryLogAnalysis = new GlideRecord('x_qune_da_log_analysis'); // UPDATE TABLE
grDiscoveryLogAnalysis.addQuery('troubleshooting.number', 'TRBL0001033');
grDiscoveryLogAnalysis.query();
while (grDiscoveryLogAnalysis.next()) {
//gs.log(grDiscoveryLogAnalysis.device_ip_address);
ipAddress = grDiscoveryLogAnalysis.getValue('device_ip_address');
arrLaIpAddress.push(ipAddress);
}
//gs.log(arrLaIpAddress);
var grCmdb = new GlideRecord('cmdb_ci');
grCmdb.addNotNullQuery('ip_address');
grCmdb.query();
while (grCmdb.next()) {
var cmdbIpaddress = grCmdb.ip_address;
arrCmdbIpAddress.push(cmdbIpaddress+'\n');
}
//gs.log(arrCmdbIpAddress);
for(var y = 0; y < arrLaIpAddress.length; y++){
if(arrCmdbIpAddress.indexOf(arrLaIpAddress[y]) < 0){
arrIpAddress.push(arrLaIpAddress[y]+'\n');
}
}
gs.log(arrIpAddress);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 05:57 AM
Have you tried using the ServiceNow ArrayUtil method?
There's a built in 'diff' function that will compare two arrays. https://docs.servicenow.com/bundle/paris-application-development/page/app-store/dev_portal/API_refer...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 07:50 AM
please check script below and it gave me the difference as 1
var arrayUtil = new ArrayUtil();
var a = [1,2,3,4];
var b = [2,3,4,5];
gs.info(arrayUtil.diff(a, b));
Output:
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-18-2020 06:36 AM
Hey dhathrianoop,
Check this thread, it might help you:
Mark it Correct or Helpful, if it works based on impact....!!!!
Best Regards,
Namrata.