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
Shahida RobertsonShahida Robertson 

How to Copy Contact Email to Custom Account field?

Hello,
Disclaimer* I am an Admin with no knowledge of code, but I have to build a process and flow. In order to do this, I need this trigger to accomplish. I have created a custom field on the Account object called "Primary Contact Email." I need a trigger that will update this field with the Primary Contact's (lookup on the Account) email. Right now I have: 

trigger UpdateAccountEmails on Contact (after update) {
    Map<Id, Account> m = new Map<Id, Account>();
    for(Contact c : Trigger.new){
        if (Trigger.oldMap.get(c.Id).Email != c.Email) {
            m.put(c.AccountId, new Account(Id = c.AccountId, Primary_Contact_Email__c = c.Email));
        }
    }
    update m.values();

Can anyone help me? Thanks!
Derrick Abbey 21Derrick Abbey 21
Although a trigger would handle this, I think it would be better to use the process builder to accomplish this.  Generally you want to use Apex when declarative methods won't accomplish what you need.

So in your process, the object would be a contact, your criteria for triggering the action would be:
  • Email Is Changed Equals True. 
  • You might want a criteria to specify that this contact is the primary as opposed to any other contact on the account.
Your immediate action would be to update a record related to the contact.  Choose the account. then set your field(s):
  • Primary Contact Email - Type: Field Reference - Value: [Contact].Email