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
Jaye LoweJaye Lowe 

Update Account Email field based on Updated related Contact record

I have a directive to update an email field on the Account Record called Primary Email with the contents of the standard Email field on a related Contact Record Contact when a checkbox, Primary Client Referral, on the same contact record is checked. Not sure what the best...easiest way to accomplish this is. Any thoughts would be supremely helpul and immensely appreciated.  Thanks in advance!
Best Answer chosen by Jaye Lowe
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jaye,

The easiest way and best way would be using flow because as it is configurable we can even deactivate or activate when ever needed in production also there is no need of test class for this to deploy to production.

Go to setup> search for flow and click on new flow and select record triggered flow.

User-added imageThe formula above is OR(ISNEW(), ISCHANGED({!$Record.Primary_Client_Referral__c}))

Now click on Plus sysmbol and add get record elements and fill details as below

User-added image
Now click on Plus symbol and click on update records and fill the details as below.

User-added image
Now Save the flow with some Name and activate it.The Flow will look as below.

User-added image


Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Abdul KhatriAbdul Khatri
Hi Jaye,

One was is from Trigger, here is the code. Please confirm the custom fields, I just fake the names per you description you may need to update those.
trigger ContactAccountEmail on Contact (after insert, after update)
 {
    Map<Id, Contact> idAcctContactMap = new Map<Id, Contact>();
    for ( Contact objContact : trigger.new ) {
        
        if((trigger.oldMap == null || trigger.oldMap.get(objContact.Id).Primar_Client_Referral__c != objContact.Primar_Client_Referral__c) 
           	&& objContact.Primar_Client_Referral__c)
        {        
            idAcctContactMap.put(objContact.AccountId, objContact);
        }
    }

    List <Account> listAccounts = new List <Account>();    
    if ( idAcctContactMap != null && idAcctContactMap.size() > 0 ) {
        
        for(Contact cont : idAcctContactMap.values()){
            
            Account acct = new Account (Id = cont.AccountId);
            acct.Primary_Email__c = cont.Email;          
            
            listAccounts.add(acct);
        }

        update listAccounts;
    }   
}

Hope this help
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jaye,

The easiest way and best way would be using flow because as it is configurable we can even deactivate or activate when ever needed in production also there is no need of test class for this to deploy to production.

Go to setup> search for flow and click on new flow and select record triggered flow.

User-added imageThe formula above is OR(ISNEW(), ISCHANGED({!$Record.Primary_Client_Referral__c}))

Now click on Plus sysmbol and add get record elements and fill details as below

User-added image
Now click on Plus symbol and click on update records and fill the details as below.

User-added image
Now Save the flow with some Name and activate it.The Flow will look as below.

User-added image


Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Jaye LoweJaye Lowe
Thank you both for such great insight. Sai Praveen I used the Flow for the reasons you mentioned and it works beautifully. Thank you both so much for all the effort you put in for such great, thoughtful answers. Truly appreaciate it. Thanks!