We are trying to compile a broker report detailing commissions on a per broker basis (one email per broker). A single broker may be linked to multiple opportunities via the opportunity's contact roles i.e. the broker may appear on multiple opportunity pages as "Referral Source" under the opportunity's contact roles.

We'd like to set up an email template that displays a list of all the opportunities that the contact is listed as "Referral Source" in the contact roles.

I think that we can query for a list of opportunities that the broker is related to using the following advice: https://developer.salesforce.com/forums/?id=906F00000008zWVIAY

However, I am unsure how to incorporate that into the Visualforce email template below. We are looking to have a table listing all of the broker's referred opportunities along with the related financials in the email.

Any help is appreciated and thanks so much for y'alls time.

<messaging:emailTemplate subject="Broker Report" recipientType="Contact"   relatedToType="Opportunity">
<messaging:plainTextEmailBody >
<STYLE type="text/css">
TH {font-size: 11px; font-face: arial; background: #CCCCCC;
border-width: 1; text-align: center}
TD {font-size: 11px; font-face: verdana}
TABLE {border: solid #CCCCCC; border-width: 1}
TR {border: solid #CCCCCC; border-width: 1}
<font face = "arial" size="2">
<p>Dear , </p>
<p>Below is a detail of your commission report for this month. </p>
<table border="0">
<tr >
<th>Fees Earned</th>
<apex:repeat var="ox" value = "{!relatedTo.Account}">
<p />

1 Answer 1


Yes, you can use the OpportunityContactRoles relationship:

<messaging:emailTemplate subject="Test" recipientType="Contact" relatedToType="Opportunity">
<messaging:plainTextEmailBody >
<apex:repeat value="{!recipient.OpportunityContactRoles}" var="contactRole">
  You are related to the following opportunities:
  <apex:outputText rendered="{!contactRole.Role='Referral Source'}">
    {!contactRole.Opportunity.Name} - {!contactRole.Opportunity.Amount}

Adjust the merge fields to suit your preferences.

  • Thanks for the quick response. I made some adjustments and am being hit with the following error message: [Error: Unknown property 'core.email.template.EmailTemplateComponentController.contactRole']. Would you happen to know a workaround for this? I should say that I am certainly not qualified to be doing this, so I may be missing something rather apparent. Thanks.
    – Conner
    Commented Jan 11, 2017 at 20:12
  • @Conner contactRole should be whatever value you placed in apex:repeat or apex:dataTable's var attribute. Copy my example code directly and start from there.
    – sfdcfox
    Commented Jan 11, 2017 at 20:13
  • Never mind, I was missing something obvious. However, when I save the template, the plain text preview is simply a display of the code.
    – Conner
    Commented Jan 11, 2017 at 20:14
  • @Conner Yes, that's expected. Use the Merge and Send Test button to view a live preview.
    – sfdcfox
    Commented Jan 11, 2017 at 20:15
  • That's good to know, thanks. I've gone ahead with that and it is asking for the 'OpportunityContactRole ID'. I've inputted the 'Contact ID' pulled from a report that is related to the contact I selected, but am receiving an error message stating that the ID is invalid.
    – Conner
    Commented Jan 11, 2017 at 20:24

You must log in to answer this question.

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