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
KSorensonKSorenson 

Trigger to copy email from contact to account

Hello,

First of all, I am just the administrator, not a developer and not a coder. However today I am wearing all the hats.  I am hoping someone would be kind enough to help me with the following. I am not sure of even where to start, except that I should do this by a trigger. However I know nothing of how to actually accomplish this. I am hoping that someone could provide the code necessary as I am not savvy enough to write any of it myself. Being a non-profit, we do not have the budget to enlist a professional for this and the task has fallen to me to handle.

Short story - Due to a third party integration, we found that we needed an email address on the account page. However, with the one-to-one model, we are essentially unable to access the true Account record for an individual.  What I would like to accomplish is this:  a trigger that will copy the email address from our Contact object into our email field on the Account object. This would happen when the field is changed in any way.

I hope that I've provided enough information, but if not I will gladly elaborate where needed. Thanks in advance everyone.
Best Answer chosen by KSorenson
Akhil AnilAkhil Anil
Hi Karen,

Here goes the trigger ! I would suggest you to go through the trail head modules of Salesforce which will help you speed up your learning process in Salesforce
 
trigger CoprEmail on Contact (after insert, after update) {
    
    List<Account> acclist = new List<Account>();
    
    for(Contact c:Trigger.New) {
        Account a = new Account();
        a.Id = c.AccountId;
        a.Email__c = c.Email;
        acclist.add(a);
    }
    
    update acclist;

}

Kindly mark it as an answer if that resolves your issue !
 

All Answers

Akhil AnilAkhil Anil
Hi Karen,

Here goes the trigger ! I would suggest you to go through the trail head modules of Salesforce which will help you speed up your learning process in Salesforce
 
trigger CoprEmail on Contact (after insert, after update) {
    
    List<Account> acclist = new List<Account>();
    
    for(Contact c:Trigger.New) {
        Account a = new Account();
        a.Id = c.AccountId;
        a.Email__c = c.Email;
        acclist.add(a);
    }
    
    update acclist;

}

Kindly mark it as an answer if that resolves your issue !
 
This was selected as the best answer
vijayabakarareddy yarrabothulavijayabakarareddy yarrabothula
hi  @ Akhil Anil

I think  it will throw error if  we did't give any Account Name 



 
Akhil AnilAkhil Anil
@Vijay : That scenario is not applicable here because Karen doesn't seem to use private Contacts (Contacts without Accounts). 
KSorensonKSorenson
Thank you, Akhil, that worked as desired.  Thank you for taking the time to assist and discuss, everyone.