1

Thanks to Boris Bachovski , I was able to successfully get my VisualForce email to compile.

However, when I fired off a test Email Alert, (this action was linked with the VF Template) the Opportunity's related Opportunities were not displayed in the email. Here's part of the HTML code (I use CSS classes, and then hardcode CSS styling inline so GMAIL clients don't strip it out):

  <tr>
    <td style="padding:5px 40px 15px 40px; background-color:#fafafa; font-family:'Lato',Arial,sans-serif; font-size:14px;">
      <table width="75%" border="0" class="sfListTable" style="border: solid #ECECEC; border-width: 1px; background-color: #fafafa;">
        <tbody>
           <tr>
            <th class="sfListHead" style="color:F4F4F4; background-color:#3e8acc; border: solid #ECECEC; border-width:0px; font-family:'Lato',Arial,sans-serif; font-size: 12px;">Opp Link</th>
            <th class="sfListHead" style="color:ffffff; background-color:#3e8acc; border: solid #ECECEC; border-width:0px; font-family:'Lato',Arial,sans-serif; font-size:12px;">Amount</th>
            <th class="sfListHead" style="color:ffffff; background-color:#3e8acc; border: solid #ECECEC; border-width:0px; font-family:'Lato',Arial,sans-serif; font-size:12px;">Stage</th>
            <th class="sfListHead" style="color:ffffff; background-color:#3e8acc; border: solid #ECECEC; border-width:0px; font-family:'Lato',Arial,sans-serif; font-size:12px;">Opp Type</th>
           </tr> 
        <apex:repeat var="oppx" value="{!relatedTo.MSA_Lookup__r}">
           <tr>
              <td class="sfListCell" style="whitespace: nowrap;"><apex:outputLink value="https://na12.salesforce.com/{!oppx.id}" id="oppDetail" style="color:#3E8ACC; border: solid #ECECEC; border-width: 1px; padding: 2px; background-color:#ffffff; font-size: 12p; font-family:'Lato',Arial,sans-serif;">Detail Link</apex:outputLink></td>
              <td class="sfListCell" style="color:#3E8ACC; border: solid #ECECEC; border-width: 1px; padding: 2px; background-color:#ffffff; font-size: 12p; font-family:'Lato',Arial,sans-serif;">{!oppx.Amount}</td>
              <td class="sfListCell" style="color:#3E8ACC; border: solid #ECECEC; border-width: 1px; padding: 2px; background-color:#ffffff; font-size: 12p; font-family:'Lato',Arial,sans-serif;">{!oppx.StageName}</td>
              <td class="sfListCell" style="color:#3E8ACC; border: solid #ECECEC; border-width: 1px; padding: 2px; background-color:#ffffff; font-size: 12p; font-family:'Lato',Arial,sans-serif;">{!oppx.Primary_Media_Opportunity__c}</td> 
           </tr>
        </apex:repeat>
        </tbody>
      </table>
    </td>
  </tr>

As you can see, I reference the related Object I want to display in this HTML table with the apex markup:

<apex:repeat var="oppx" value="{!relatedTo.MSA_Lookup__r}">
...
</apex:repeat>

Here is how the HTML email rendered (I already corrected the merge fields, so the actual Object name is passed in, not the Id): Imgur Link

What is my VisuaForce markup missing to successfully get the related objects (Opportunities in this case) to correctly display?

3

It's not an issue with <apex:repeat>. It is because you can't reference child objects by using related.MSA_Lookup__r. This will not work. You need to have a controller and populate that up in the controller. Here is the link for how to do it: Access child object's fields in visualforce email template

  • thanks for the headsup-- to be clear, these Opportunities are not 'child objects', they are related to the Opportunity in question via a lookup. Not sure if that makes a difference but wanted to be clear – GPP Jan 13 '16 at 4:01
  • 1
    @GPP technically, that's how we define 'child objects' – Lance Shi Jan 13 '16 at 4:02
  • I just tested another alternative and realized that in this instance, no custom controller was needed! I was not referencing the correct relationship field the Opportunity has with a 'Child' Opp: I was using the Lookup field and swapping the '__c' for an '__r' when in fact it was a separate field that should have been referenced: MSA_Opportunities__r – GPP Jun 13 '16 at 1:40
  • will I need to write a custom controller for referencing custom child objects or can I just use the (correct) reference field? – GPP Jun 13 '16 at 1:55
0

You don't need to use apex: repeat in this case if there this is just one related record. Repeat is used when you have list of records and you want to print all of them. Try to remove repeat and instead do this:

       <tr>
          <td class="sfListCell" style="whitespace: nowrap;"><apex:outputLink value="https://na12.salesforce.com/{!relatedTo.MSA_Lookup__c}" id="oppDetail" style="color:#3E8ACC; border: solid #ECECEC; border-width: 1px; padding: 2px; background-color:#ffffff; font-size: 12p; font-family:'Lato',Arial,sans-serif;">Detail Link</apex:outputLink></td>
          <td class="sfListCell" style="color:#3E8ACC; border: solid #ECECEC; border-width: 1px; padding: 2px; background-color:#ffffff; font-size: 12p; font-family:'Lato',Arial,sans-serif;">{!relatedTo.MSA_Lookup__r.Amount}</td>
          <td class="sfListCell" style="color:#3E8ACC; border: solid #ECECEC; border-width: 1px; padding: 2px; background-color:#ffffff; font-size: 12p; font-family:'Lato',Arial,sans-serif;">{!relatedTo.MSA_Lookup__r.StageName}</td>
          <td class="sfListCell" style="color:#3E8ACC; border: solid #ECECEC; border-width: 1px; padding: 2px; background-color:#ffffff; font-size: 12p; font-family:'Lato',Arial,sans-serif;">{!relatedTo.MSA_Lookup__r.Primary_Media_Opportunity__c}</td> 
       </tr>
  • thanks for the response, there are instances where this Opportunity will have several Opportunities that are related to it. – GPP Jan 13 '16 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.