- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2022 02:18 PM
Hi,
I need to compare 2 arrays. Find the sysids from array Array1 and see if they exist in Array2. If they don't add them to array3. The arrays are displaying correctly but when I try to log array3 to find the difference, it just seems to be logging the values in array1. Code below. Tried using the arrayUtil as well but was getting the same result
var array1 = [];
var array2 = [];
var array3 = [];
var legalEnt = new GlideRecord('u_department_mapping_rules');
legalEnt.addQuery('u_department_name', current.u_department_name);
legalEnt.query();
while (legalEnt.next()) {
array1.push(legalEnt.u_linked_legal_entity.toString());
}
gs.log("AB1601 array is " + array1);
var compM2m = new GlideRecord('u_m2m_company_department');
compM2m.addQuery('u_department', current.u_department_name);
compM2m.query();
while (compM2m.next()) {
array2.push(compM2m.u_company.sys_id);
}
for(var i = 0; i < array1.length; i++){
if(array2.indexOf(array1[i]) == -1) {
array3.push(array1[i]);
}
}
gs.log("AB160199 " + array3);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2022 02:52 PM
can you give this a go
var array1 = [];
var array2 = [];
var array3 = [];
var legalEnt = new GlideRecord('u_department_mapping_rules');
legalEnt.addQuery('u_department_name', current.u_department_name);
legalEnt.query();
while (legalEnt.next()) {
array1.push(legalEnt.u_linked_legal_entity.toString());
}
gs.log("AB1601 array is " + array1);
var compM2m = new GlideRecord('u_m2m_company_department');
compM2m.addQuery('u_department', current.u_department_name);
compM2m.query();
while (compM2m.next()) {
array2.push(compM2m.u_company.sys_id +'');
}
for(var i = 0; i < array1.length; i++){
if(array2.indexOf(array1[i]) == -1) {
array3.push(array1[i] +'');
}
}
gs.log("AB160199 " + array3);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2022 02:34 PM
have you tried to write your code like this
var array1 = [];
var array2 = [];
var array3 = [];
var legalEnt = new GlideRecord('u_department_mapping_rules');
legalEnt.addQuery('u_department_name', current.u_department_name);
legalEnt.query();
while (legalEnt.next()) {
array1.push(legalEnt.u_linked_legal_entity.toString());
}
gs.log("AB1601 array is " + array1);
var compM2m = new GlideRecord('u_m2m_company_department');
compM2m.addQuery('u_department', current.u_department_name);
compM2m.query();
while (compM2m.next()) {
array2.push(compM2m.u_company.sys_id);
}
for(var i = 0; i < array1.length; i++){
if(array2.indexOf(array1[i]) == -1) {
array3.push(array1[i] +'');
}
}
gs.log("AB160199 " + array3);
Please mark my answer as ✅ Correct / Helpful based on the Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2022 02:39 PM
Hi Ahmed
Thanks for the response. Still just seems to log the sysids of array1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2022 02:52 PM
can you give this a go
var array1 = [];
var array2 = [];
var array3 = [];
var legalEnt = new GlideRecord('u_department_mapping_rules');
legalEnt.addQuery('u_department_name', current.u_department_name);
legalEnt.query();
while (legalEnt.next()) {
array1.push(legalEnt.u_linked_legal_entity.toString());
}
gs.log("AB1601 array is " + array1);
var compM2m = new GlideRecord('u_m2m_company_department');
compM2m.addQuery('u_department', current.u_department_name);
compM2m.query();
while (compM2m.next()) {
array2.push(compM2m.u_company.sys_id +'');
}
for(var i = 0; i < array1.length; i++){
if(array2.indexOf(array1[i]) == -1) {
array3.push(array1[i] +'');
}
}
gs.log("AB160199 " + array3);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2022 03:01 PM
Thank you Ahmed! What was I doing wrong?