function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BrianHBrianH 

Best way to combine lists of contacts

What would the best way to combine 4 separate lists into 1 in this scenario?  (These lists all contain contact objects.)

 

List1 - Contact ID and Field1

List2 - Contact ID and Field2

List3 - Contact ID and FIeld3

List4 - Contact ID and FIeld4

 

I would then need List5 to contain all contacts from Lists1-4 in with all fields.

 

List5 - Contact ID, Field1, Field2, Field3, Field4

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Ok now i got your problem. now there are two solutions for your problem.

1) Use merge : This will only work if your list size is very less, other wise dml limit will exceed. This is not the best way to do it. Please check contact is mergable before going for this solution. I think it is.

 

2)

Step 1 : Create a map from first all four lists for unique items and remove them from original list.

Step 2 : If any item comes again already existing in map then check for  all the fields on contact, if any field is empty then copyfield value from the list item to map item. You will have to use sObject methods for this

 

 

if(sObjectFromMap.get(FieldAPIName) == null)

    sObject.put(FieldAPIName , sObjectDuplicate.get(FieldAPIName))

 

Repeat this on all lists, your map will have unique items with all the values.

 

All Answers

Shashikant SharmaShashikant Sharma

Use this 

 

 

List5.addAll(List1);
List5.addAll(List2);
List5.addAll(List3);
List5.addAll(List4);

 

 

 

List Method : addAll , 

 

 

Adds all of the elements in list l to the list that calls the method. Note that both lists must be of the same type. Adds all of the elements in list l to the list that calls themethod. Note that both lists must be of the same type.

 

BrianHBrianH

Addall doesn't quite do what I need.  Using that method, if List1 and List2 contained the same contact, that contact would be in List5 twice.  I need the contacts to be merged if they exist in multiple lists.

Shashikant SharmaShashikant Sharma

In this case create a

Set<Contact> setUniqueCon =  new Set<Contact>();

setUniqueCon.addAll(list1);

setUniqueCon.addAll(list2);

setUniqueCon.addAll(list3);

setUniqueCon.addAll(list4);

 

You can use this set or  you can copy this to your list5

 

list5.addall(setUniqueCon);

 

BrianHBrianH

That's still not quite what I need.


If List1 contains a contact and List2 contains the same contact but with different fields, then it still gets added to the set as a separate item.

 

What I need to happen is as follows.

 

List1

 

Id     FName
1      Brian
2      Joe

 List2

Id      LName
1       Smith
2       Johnson
3 Black

 

My result needs to be:

List3

 

Id      FName      LName
1       Brian      Smith
2       Joe        Johnson
3                  Black

 

 

 

 

Shashikant SharmaShashikant Sharma

Ok now i got your problem. now there are two solutions for your problem.

1) Use merge : This will only work if your list size is very less, other wise dml limit will exceed. This is not the best way to do it. Please check contact is mergable before going for this solution. I think it is.

 

2)

Step 1 : Create a map from first all four lists for unique items and remove them from original list.

Step 2 : If any item comes again already existing in map then check for  all the fields on contact, if any field is empty then copyfield value from the list item to map item. You will have to use sObject methods for this

 

 

if(sObjectFromMap.get(FieldAPIName) == null)

    sObject.put(FieldAPIName , sObjectDuplicate.get(FieldAPIName))

 

Repeat this on all lists, your map will have unique items with all the values.

 

This was selected as the best answer