How to efficiently combine 2 Lists if the same Contact is in both
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
I have 2 lists of Contacts, which may both contain the same Contact(s) or they may not. I have a method that combines the lists, meaning if a Contact is in both lists it will take the Contact's properties of that one list and add those properties to the matching Contact in the other list and then add the Contact to one final list (finalContactUpdates
). I am wondering if there is an easier way of doing it?
public static List<Contact> mergeContactLists(List<Contact> expiryDates, List<Contact> primaryCommunities)
List<Contact> finalContactUpdates = new List<Contact>();
Map<Id, Contact> contactIdToContact = new Map<Id, Contact>();
for(Contact con : expiryDates)
contactIdToContact.put(con.Id, con);
Map<Id, Contact> contactIdToContact2 = new Map<Id, Contact>();
for(Contact con : primaryCommunities)
contactIdToContact2.put(con.Id, con);
for(Contact con : primaryCommunities)
if(contactIdToContact.keySet().contains(con.Id))
//if a contact who's primaryCommunity changed also has an expiry date that changed, set the expiry date
con.Membership_Expiry_Date__c = contactIdToContact.get(con.Id).Membership_Expiry_Date__c;
finalContactUpdates.add(con);
for(Contact con : expiryDates)
if(!contactIdToContact2.keySet().contains(con.Id))
//if a contact's expiry date changed but primaryCommunity did not, add them to the contact list
finalContactUpdates.add(con);
return finalContactUpdates;
apex list contact
add a comment |Â
up vote
4
down vote
favorite
I have 2 lists of Contacts, which may both contain the same Contact(s) or they may not. I have a method that combines the lists, meaning if a Contact is in both lists it will take the Contact's properties of that one list and add those properties to the matching Contact in the other list and then add the Contact to one final list (finalContactUpdates
). I am wondering if there is an easier way of doing it?
public static List<Contact> mergeContactLists(List<Contact> expiryDates, List<Contact> primaryCommunities)
List<Contact> finalContactUpdates = new List<Contact>();
Map<Id, Contact> contactIdToContact = new Map<Id, Contact>();
for(Contact con : expiryDates)
contactIdToContact.put(con.Id, con);
Map<Id, Contact> contactIdToContact2 = new Map<Id, Contact>();
for(Contact con : primaryCommunities)
contactIdToContact2.put(con.Id, con);
for(Contact con : primaryCommunities)
if(contactIdToContact.keySet().contains(con.Id))
//if a contact who's primaryCommunity changed also has an expiry date that changed, set the expiry date
con.Membership_Expiry_Date__c = contactIdToContact.get(con.Id).Membership_Expiry_Date__c;
finalContactUpdates.add(con);
for(Contact con : expiryDates)
if(!contactIdToContact2.keySet().contains(con.Id))
//if a contact's expiry date changed but primaryCommunity did not, add them to the contact list
finalContactUpdates.add(con);
return finalContactUpdates;
apex list contact
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have 2 lists of Contacts, which may both contain the same Contact(s) or they may not. I have a method that combines the lists, meaning if a Contact is in both lists it will take the Contact's properties of that one list and add those properties to the matching Contact in the other list and then add the Contact to one final list (finalContactUpdates
). I am wondering if there is an easier way of doing it?
public static List<Contact> mergeContactLists(List<Contact> expiryDates, List<Contact> primaryCommunities)
List<Contact> finalContactUpdates = new List<Contact>();
Map<Id, Contact> contactIdToContact = new Map<Id, Contact>();
for(Contact con : expiryDates)
contactIdToContact.put(con.Id, con);
Map<Id, Contact> contactIdToContact2 = new Map<Id, Contact>();
for(Contact con : primaryCommunities)
contactIdToContact2.put(con.Id, con);
for(Contact con : primaryCommunities)
if(contactIdToContact.keySet().contains(con.Id))
//if a contact who's primaryCommunity changed also has an expiry date that changed, set the expiry date
con.Membership_Expiry_Date__c = contactIdToContact.get(con.Id).Membership_Expiry_Date__c;
finalContactUpdates.add(con);
for(Contact con : expiryDates)
if(!contactIdToContact2.keySet().contains(con.Id))
//if a contact's expiry date changed but primaryCommunity did not, add them to the contact list
finalContactUpdates.add(con);
return finalContactUpdates;
apex list contact
I have 2 lists of Contacts, which may both contain the same Contact(s) or they may not. I have a method that combines the lists, meaning if a Contact is in both lists it will take the Contact's properties of that one list and add those properties to the matching Contact in the other list and then add the Contact to one final list (finalContactUpdates
). I am wondering if there is an easier way of doing it?
public static List<Contact> mergeContactLists(List<Contact> expiryDates, List<Contact> primaryCommunities)
List<Contact> finalContactUpdates = new List<Contact>();
Map<Id, Contact> contactIdToContact = new Map<Id, Contact>();
for(Contact con : expiryDates)
contactIdToContact.put(con.Id, con);
Map<Id, Contact> contactIdToContact2 = new Map<Id, Contact>();
for(Contact con : primaryCommunities)
contactIdToContact2.put(con.Id, con);
for(Contact con : primaryCommunities)
if(contactIdToContact.keySet().contains(con.Id))
//if a contact who's primaryCommunity changed also has an expiry date that changed, set the expiry date
con.Membership_Expiry_Date__c = contactIdToContact.get(con.Id).Membership_Expiry_Date__c;
finalContactUpdates.add(con);
for(Contact con : expiryDates)
if(!contactIdToContact2.keySet().contains(con.Id))
//if a contact's expiry date changed but primaryCommunity did not, add them to the contact list
finalContactUpdates.add(con);
return finalContactUpdates;
apex list contact
asked Aug 28 at 17:46
tom_12321
486
486
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
Here's how'd I do it:
public static SObject mergeData(SObject list1, SObject list2)
Map<Id, SObject> results = new Map<Id, SObject>();
for(SObject recordList: new List<SObject> list2, list1 )
for(SObject record: recordList)
results.put(record.Id, record.getSObjectType().newSobject(record.Id));
for(SObject recordList: new List<SObject> list2, list1 )
for(Sobject record: recordList)
Map<String, Object> values = record.getPopulatedFieldsAsMap();
SObject temp = results.get(record.Id);
for(String field: values.keySet())
temp.put(field, values.get(field));
SObject finalResults = list1.clone();
finalResults.clear();
finalResults.addAll(results.values());
return finalResults;
This code builds a map of records, takes all the populated field values from both lists, merges them into a single record, and returns the results. Note that list1 and list2 should have the same data type, or Bad Things⢠might happen.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
Here's how'd I do it:
public static SObject mergeData(SObject list1, SObject list2)
Map<Id, SObject> results = new Map<Id, SObject>();
for(SObject recordList: new List<SObject> list2, list1 )
for(SObject record: recordList)
results.put(record.Id, record.getSObjectType().newSobject(record.Id));
for(SObject recordList: new List<SObject> list2, list1 )
for(Sobject record: recordList)
Map<String, Object> values = record.getPopulatedFieldsAsMap();
SObject temp = results.get(record.Id);
for(String field: values.keySet())
temp.put(field, values.get(field));
SObject finalResults = list1.clone();
finalResults.clear();
finalResults.addAll(results.values());
return finalResults;
This code builds a map of records, takes all the populated field values from both lists, merges them into a single record, and returns the results. Note that list1 and list2 should have the same data type, or Bad Things⢠might happen.
add a comment |Â
up vote
7
down vote
accepted
Here's how'd I do it:
public static SObject mergeData(SObject list1, SObject list2)
Map<Id, SObject> results = new Map<Id, SObject>();
for(SObject recordList: new List<SObject> list2, list1 )
for(SObject record: recordList)
results.put(record.Id, record.getSObjectType().newSobject(record.Id));
for(SObject recordList: new List<SObject> list2, list1 )
for(Sobject record: recordList)
Map<String, Object> values = record.getPopulatedFieldsAsMap();
SObject temp = results.get(record.Id);
for(String field: values.keySet())
temp.put(field, values.get(field));
SObject finalResults = list1.clone();
finalResults.clear();
finalResults.addAll(results.values());
return finalResults;
This code builds a map of records, takes all the populated field values from both lists, merges them into a single record, and returns the results. Note that list1 and list2 should have the same data type, or Bad Things⢠might happen.
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
Here's how'd I do it:
public static SObject mergeData(SObject list1, SObject list2)
Map<Id, SObject> results = new Map<Id, SObject>();
for(SObject recordList: new List<SObject> list2, list1 )
for(SObject record: recordList)
results.put(record.Id, record.getSObjectType().newSobject(record.Id));
for(SObject recordList: new List<SObject> list2, list1 )
for(Sobject record: recordList)
Map<String, Object> values = record.getPopulatedFieldsAsMap();
SObject temp = results.get(record.Id);
for(String field: values.keySet())
temp.put(field, values.get(field));
SObject finalResults = list1.clone();
finalResults.clear();
finalResults.addAll(results.values());
return finalResults;
This code builds a map of records, takes all the populated field values from both lists, merges them into a single record, and returns the results. Note that list1 and list2 should have the same data type, or Bad Things⢠might happen.
Here's how'd I do it:
public static SObject mergeData(SObject list1, SObject list2)
Map<Id, SObject> results = new Map<Id, SObject>();
for(SObject recordList: new List<SObject> list2, list1 )
for(SObject record: recordList)
results.put(record.Id, record.getSObjectType().newSobject(record.Id));
for(SObject recordList: new List<SObject> list2, list1 )
for(Sobject record: recordList)
Map<String, Object> values = record.getPopulatedFieldsAsMap();
SObject temp = results.get(record.Id);
for(String field: values.keySet())
temp.put(field, values.get(field));
SObject finalResults = list1.clone();
finalResults.clear();
finalResults.addAll(results.values());
return finalResults;
This code builds a map of records, takes all the populated field values from both lists, merges them into a single record, and returns the results. Note that list1 and list2 should have the same data type, or Bad Things⢠might happen.
answered Aug 28 at 18:03
sfdcfox
226k10172387
226k10172387
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f230398%2fhow-to-efficiently-combine-2-listscontact-if-the-same-contact-is-in-both%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password