+ Start a Discussion
JkkJkk 

trigger to populate latest contact record's email id on account field?

Please help me with a trigger to populate latest contact record's email id on account field
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CopyConToAcc on Contact (after insert) {
    
    List<Id> accIds = new List<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Contact c : trigger.new){
        accIds.add(c.accountId);
    }
    
    for(Account acc : [SELECT Id, Email__c FROM Account WHERE Id IN :accIds]){
        for(Contact con:  trigger.new){
            acc.Email__c = con.Email;
            accounts.add(acc);
        }
    }
    if(accounts.size()>0){
        Update accounts;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
JkkJkk
Khan Anas (https://dfc-org-production.force.com/forums/ForumsProfile?communityId=09aF00000004HMG&userId=0050G00000B1Cfw&showHeader=false) I want the latest contact's email id. Consider there is contact1 created yesterday and contact2 created today, so i want contact2's email id to be populated.
Ajay K DubediAjay K Dubedi
Hi,
Try the below code it works for me:
Trigger:
trigger YaTriggerContact on Contact (before insert) {
        if(Trigger.isInsert){
            YaTestClassConatct.updateConatctAccount(trigger.new);
    }
}
Trigger-Handler:
public class YaTestClassConatct {
    public static void updateConatctAccount(List<Contact> conList){
        if(conList.size()>0){
            try{
                Set<Id> acIdSet=new Set<Id>();
                Map<Id,String> accountVsConatctMap=new Map<Id,String>();
                List<Account> accList=new List<Account>();
                for(Contact con:conList){
                    if(con.AccountId!=null){
                        acIdSet.add(con.AccountId);
                        accountVsConatctMap.put(con.AccountId,con.Email);
                    }
                }
                if(acIdSet.size()>0){
                    accList=[Select Id,Name,Email__c From Account Where Id In:acIdSet Limit 10000];
                }
                if(accList.size()>0){
                    for(Account acc:accList){
                        if(accountVsConatctMap.containsKey(acc.Id)){
                            acc.Email__c=accountVsConatctMap.get(acc.Id);
                        }
                    }
                }
                update accList;
            }
           catch (Exception ex) {
            system.debug('Error->' + ex.getMessage() + '--LineNo->' + ex.getLineNumber());
        }
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi