- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2024 12:29 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2024 12:44 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2024 12:44 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2024 01:09 AM
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