How to efficiently combine 2 Lists if the same Contact is in both

The name of the pictureThe name of the pictureThe name of the pictureClash 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;







share|improve this question


























    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;







    share|improve this question






















      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;







      share|improve this question












      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;









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 28 at 17:46









      tom_12321

      486




      486




















          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.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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.






            share|improve this answer
























              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.






              share|improve this answer






















                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 28 at 18:03









                sfdcfox

                226k10172387




                226k10172387



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    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













































































                    這個網誌中的熱門文章

                    How to combine Bézier curves to a surface?

                    Mutual Information Always Non-negative

                    Why am i infinitely getting the same tweet with the Twitter Search API?