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
Gopikrishna DasariGopikrishna Dasari 

I have one question I want to write the trigger when ever I am trying to insert or update the contact I want to update the contact email with account email

any one can help me on this
Best Answer chosen by Gopikrishna Dasari
SubratSubrat (Salesforce Developers) 
Hello Gopikrishna ,

Please try with the below trigger and let me know further :
trigger UpdateContactEmail on Contact (before insert, before update) {
    // Collect the Account Ids related to the Contacts
    Set<Id> accountIds = new Set<Id>();
    for (Contact contact : Trigger.new) {
        accountIds.add(contact.AccountId);
    }
    
    // Retrieve the related Accounts and their emails
    Map<Id, Account> accountsMap = new Map<Id, Account>(
        [SELECT Id, Email FROM Account WHERE Id IN :accountIds]
    );
    
    // Update the Contact emails with the corresponding Account emails
    for (Contact contact : Trigger.new) {
        if (accountsMap.containsKey(contact.AccountId)) {
            contact.Email = accountsMap.get(contact.AccountId).Email;
        }
    }
}

Hope this helps !
Thank you.​​​​​​​

All Answers

Mukesh in CloudMukesh in Cloud
The requirement doesnt seem clear, just an Inset and update of contact why you want to update email on contact. I feel do you want to say whenever Account email get update.

I will suggest you can just create a simple formula field if you want to keep contact email same as account email.

Or else if the requirements is as you mentioned, will suggest to use Salesforce flow and keep email up to date.
Follow below link
https://help.salesforce.com/s/articleView?id=sf.flow_build_data_update.htm&type=5

If this resolved your question please dont forget to mark it as Best answer
SubratSubrat (Salesforce Developers) 
Hello Gopikrishna ,

Please try with the below trigger and let me know further :
trigger UpdateContactEmail on Contact (before insert, before update) {
    // Collect the Account Ids related to the Contacts
    Set<Id> accountIds = new Set<Id>();
    for (Contact contact : Trigger.new) {
        accountIds.add(contact.AccountId);
    }
    
    // Retrieve the related Accounts and their emails
    Map<Id, Account> accountsMap = new Map<Id, Account>(
        [SELECT Id, Email FROM Account WHERE Id IN :accountIds]
    );
    
    // Update the Contact emails with the corresponding Account emails
    for (Contact contact : Trigger.new) {
        if (accountsMap.containsKey(contact.AccountId)) {
            contact.Email = accountsMap.get(contact.AccountId).Email;
        }
    }
}

Hope this helps !
Thank you.​​​​​​​
This was selected as the best answer
priddy broderickpriddy broderick
Sure, I'd be glad to help with your question. To achieve the desired functionality, you can use an Apex trigger in Salesforce. The trigger would fire whenever a contact is inserted or updated, and then it would update the contact's email with the corresponding account email.
Here's a general outline of what the trigger could look like:
apexCopy code
trigger UpdateContactEmail on Contact (before insert, before update) { for (Contact contact : Trigger.new) { // Check if the contact has an associated account if (contact.AccountId != null) { // Get the related account record Account relatedAccount = [SELECT Id, Email FROM Account WHERE Id = :contact.AccountId LIMIT 1]; // Update the contact's email with the account's email if (relatedAccount != null && relatedAccount.Email != null) { contact.Email = relatedAccount.Email; } } } }
Please make sure to test the trigger thoroughly in a sandbox environment before deployingor try the nol monthly pass (https://nolcard.ae/nol-card-monthly-pass/) it to production. If you encounter any issues or need further assistance, feel free to ask!
Gopikrishna DasariGopikrishna Dasari
Hi Subrat, 

Thanks for your answer.

Thanks,
Gopikrishna
Gopikrishna DasariGopikrishna Dasari
Hi Mukesh,

Can you please give brief explanation how can we create flow on this requirement.
Thanks in advance.

Thanks,
Gopikrishna.