The CreatorCon Call for Content is officially open! Get started here.

How to find the values that are different between two lists?

Alon Grod
Tera Expert

Hi,

If i have two fields of type List, what is the best way to find the values that are different between the lists and save them into a new list using script?

1 ACCEPTED SOLUTION

Ranjit Nimbalka
Mega Sage

Hi @Alon Grod ,

Array Util will be best way to achieve this.

please take a look at below code you will understand.

 

 

var array1=['0a52d3dcd7011200f2d224837e6103f2','3cc3c7680b982300cac6c08393673a03','2156c3a80b982300cac6c08393673a7e'];
var array2 = ['0a52d3dcd7011200f2d224837e6103f2','3cc3c7680b982300cac6c08393673a03'];
var array3 = [];
for(var i = 0; i <= array1.length; i++)
{
if(array2.indexOf(array1[i]) == -1){
array3.push(array1[i]);

}
}
gs.info(array3)​

 

 and if you getting values from field like below

array1=requestedBy.u_groups

then always use split() functions like below

array1=requestedBy.u_groups.split(",");

 If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Ranjit

View solution in original post

2 REPLIES 2

Ranjit Nimbalka
Mega Sage

Hi @Alon Grod ,

Array Util will be best way to achieve this.

please take a look at below code you will understand.

 

 

var array1=['0a52d3dcd7011200f2d224837e6103f2','3cc3c7680b982300cac6c08393673a03','2156c3a80b982300cac6c08393673a7e'];
var array2 = ['0a52d3dcd7011200f2d224837e6103f2','3cc3c7680b982300cac6c08393673a03'];
var array3 = [];
for(var i = 0; i <= array1.length; i++)
{
if(array2.indexOf(array1[i]) == -1){
array3.push(array1[i]);

}
}
gs.info(array3)​

 

 and if you getting values from field like below

array1=requestedBy.u_groups

then always use split() functions like below

array1=requestedBy.u_groups.split(",");

 If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Ranjit

Maddysunil
Kilo Sage

@Alon Grod 

If you are using business rule, you can use below sample code:

 

// Get the current record
var currentRecord = new GlideRecord('your_table_name');
currentRecord.get('your_record_sys_id');

// Get the values of the two list fields
var listFieldA = currentRecord.getValue('list_field_a');
var listFieldB = currentRecord.getValue('list_field_b');

// Convert the list values to arrays (assuming comma-separated values)
var listValuesA = listFieldA.split(',');
var listValuesB = listFieldB.split(',');

// Initialize an empty array to store the differing values
var differingValues = [];

// Compare the values to identify the differences
listValuesA.forEach(function(value) {
    if (listValuesB.indexOf(value) === -1) {
        differingValues.push(value);
    }
});

listValuesB.forEach(function(value) {
    if (listValuesA.indexOf(value) === -1 && differingValues.indexOf(value) === -1) {
        differingValues.push(value);
    }
});

// Save the differing values into a new list
var newFieldValue = differingValues.join(',');

// Update the new list field on the current record
currentRecord.setValue('new_list_field', newFieldValue);
currentRecord.update();

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks