1

Unfortunetly Salesforce currently does not allow emails to be sent to Contact Roles in Opportunities via Workflows. (Idea Exchange link)

I would like to know if anyone else has come across this roadblock and what their solutions were to this? I need to be able to send emails to the Primary contact in my Contact Roles.

3 Answers 3

1

There isn't a simple catch-all solution. You can't reference OpportunityContactRoles in a Workflow email alert so you have to use a custom Contact lookup on the Opportunity (for example, Opportunity.Primary_OCR__c). There are a couple of ways to automate the population of this lookup but neither is foolproof.

Solution 1:

You can create a before update trigger on the Opportunity which queries OpportunityContactRole where IsPrimary = true. Be sure to bulkify your trigger and map Opportunity Ids to the located OpportunityContactRole.

There is a serious limitation to this approach, however, as it requires you to update the Opportunity record itself before it will update the Primary OCR lookup. Setting an OCR as primary does not cause the related Opportunity record to update. If your email is being sent as a time dependent action on a workflow, you will want to ensure the Opportunity record is updated just prior to the email alert.

Solution 2:

If all of your OpportunityContactRoles are going to be created via the standard Opportunity layout, you can create a custom VisualForce page and controller to do this behind the scenes anytime you navigate to an Opportunity. The controller uses an @future method which queries the primary contact roles and updates the Opportunity lookup. This method is called via the action attribute on the VisualForce page. Add the VisualForce page to all Opportunity layouts as a hidden section (height and width set to 0, do not display label or section header). You can find code for this clever workaround here, but it won't apply to OpportunityContactRoles inserted via the API. These won't be updated until a user navigates to the record of the parent Opportunity.

0

The only workaround I have found is to create two new fields

  • Primary User (Contact drop down)
  • Primary User email (formula field)

The user will manually select the primary contact to the first field (although if you wanted to you could use some apex magic to auto populate this). This will them populate the second field with the selected users email address.

In the send email settings I select the Primary User email field from the available fields as the send To address.

0

create new field in opportunity lookup with contact and using a trigger we can populate the contact role user .

Trigger CopyPrimaryContactandcontactRoleToOptyTrigger on Opportunity (before update)
{

   for (Opportunity o : Trigger.new)    
   {

       OpportunityContactRole[] contactRoleArray = [select ContactID, isPrimary, Role from OpportunityContactRole where OpportunityContactRole.Role = 'Siemens BusDev Mgr' and  OpportunityId = :o.id ORDER BY isPrimary DESC, createdDate];

       if (contactRoleArray.size() > 0)    
       {
           o.Contact_Role__c= contactRoleArray[0].ContactID; 
       }

   }
}
1
  • Depending on how often Opportunities get updated, you might be better off with schedulable/batch apex.
    – martin
    Sep 14, 2015 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .