Skip to main content
Hi SF Community - I have two contacts under the same Household and would like to pull an email address from one contact and place it on another contact.  One contact is the primary record and the other is the secondary record. 

 

The differentiator is the picklist value for each.  If the value selection of the Type of Client field is COI or Client than it is the Primary Record and if the Type of Client is Family Partner then it is always the Secondary Record.  Please let me know if there is a way for me to pull the Family Partner email (Secondary Email) onto the Primary Record.  Appreciate it.  

 

Thanks, 

 

Mike 
6 answers
  1. Jun 19, 2021, 2:55 AM
    Hi Michael,

     

    Didn't he help you? It was a nomination, so I refrained from doing so.

     

    I haven't written the test code yet, so it's not the correct answer, but it looks like this.

     

     

    trigger ContactMailaddress on Contact (after insert,after update) {

    Set<String> AccountIdSet = new Set<String>();

    for (Contact con :Trigger.new){

    if (con.Type_of_Client__c == 'Family Partner'){

    AccountIdSet.add(con.AccountId);

    }

    }//end of for

    List<Contact> ContactList = [SELECT Id,AccountId,Type_of_Client__c FROM Contact WHERE (Type_of_Client__c='COI' OR Type_of_Client__c ='Client') AND Id =: AccountIdSet];

    if (ContactList.size() < 1){

    return;

    }

    Map<Id,List<Contact>> ConatctMap = new Map<Id,List<Contact>>();

    Map<Id,String> ConatctMap2 = new Map<Id,String>();

    String AccountIdkey = '';

    String Secondary_Email = '';

    List<Contact> subContactList = new List<Contact>();

    for (Contact con : ContactList){

    if (AccountIdkey != con.AccountId){

    if (AccountIdkey != ''){

    ConatctMap.put(con.AccountId,subContactList);

    ConatctMap2.put(con.AccountId,Secondary_Email);

    }

    subContactList = new List<Contact>();

    AccountIdkey = con.AccountId;

    Secondary_Email = con.Email;

    } else {

    subContactList.add(con);

    Secondary_Email = Secondary_Email + ';' + con.Email;

    }

    }//end of for

    ConatctMap.put(AccountIdkey,subContactList);

    ConatctMap2.put(AccountIdkey,Secondary_Email);

    List<Contact> updateContactList = new List<Contact>();

    for (Contact con :Trigger.new){

    List<Contact> ConList = ConatctMap.get(con.AccountId);

    if (ConList != null){

    for (Contact myCon : ConList){

    Contact upCon = new Contact();

    upCon.Id = myCon.Id;

    String Secondary_Email =ConatctMap2.get(con.AccountId);

    if (Secondary_Email != null) upCon.Secondary_Email__c = Secondary_Email;

    updateContactList.add(upCon);

    }

    }

    }

    System.debug(Logginglevel.INFO,'=========>>>'+ updateContactList);

    if (updateContactList.size() > 0 ) update updateContactList;

    }

     

     
0/9000