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
Sales DevelopmentSales Development 

How to copy Email of Primary Contact in a new custom field in Opportunity ?

Hello everyone, 

I am trying to create a workflow or a process builder or even an APEX trigger in order to copy automatically the email of primary contact into a new Opportunity custom field when the Primary Contact is created or changed/edited.
I found a solution for this but it was not completely automatic since the custom field was a lookup field and for each opportunity, i had to select it manually, i would like this process totally automatic. I hope it is possible !

Thank you for your future answers, I hope someone can help us !
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sales,

May I suggest you to please refer the below link to copy primary contact to opportunity custom field. hope It will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar
Sales DevelopmentSales Development
Hi Rahul Kumar, thamk you for your help againm I do not know why but it does not work. I follow all the steps, no error message but it does nothing.
Do you have any idea of what is going wrong ?
Maharajan CMaharajan C
Hi Friend,

You are trying to send an email to primary contacts in closed opportunities right?

So What i done for your scenario is:

1) created the custom contact lookup field name:Primary_Contace__c.

2)update the Conatct field from the Opportunity Contact Role using below trigger.

trigger primarycontact on Opportunity (before insert,before update) {
set<Id> OppId=new set<Id>();
for(Opportunity opp:trigger.new)
{
If(opp.Isclosed == True)
{
OppId.add(opp.Id);
}
}

map<Id,Id> OPC=new map<Id,Id>();
for(OpportunityContactRole OCR:[select Id,ContactId,isPrimary,Role,OpportunityId from OpportunityContactRole where OpportunityId = :OppId and IsPrimary=True])
{
OPC.put(OCR.OpportunityId,OCR.ContactId);
}
List<Opportunity> OppUp=new List<Opportunity>();
for(opportunity opp:trigger.new)
{
//OpportunityContactRole OPPCON=OPC.get(opp.Id);
opp.Primary_Contact__c=OPC.get(opp.Id);
OppUp.add(opp);
}
//Update OppUp;
}

3) create the Workflow and in the Email Alert use the Recipient type as Related Contact.
   
Then select the Related Contact: Primary Contact to Selected Recipients.

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
​Raj