2

We're trying to use apex:composition in a Visualforce email template. This works fine if I simply do this:

<messaging:emailTemplate subject="Template Example" recipientType="Contact">
    <messaging:htmlEmailBody >
        <apex:composition template="EmailTemplate">
            <apex:define name="body">
                This is the example body
            </apex:define>
        </apex:composition>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>

But if I start to use any Visualforce merge tag or Visualforce component, then Salesforce won't compile the email template.

<messaging:emailTemplate subject="Template Example" recipientType="Contact">
    <messaging:htmlEmailBody >
        <apex:composition template="EmailTemplate">
            <apex:define name="body">
                Hi {!recipient.FirstName}
            </apex:define>
        </apex:composition>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>

Salesforce will return 'Error: An unexpected error has occurred' when I try to save the email template. Is this known behaviour? What can I do to avoid it?

  • I've ran into this issue as well, were you ever able to find a resolution? – Mattisdada Dec 6 '18 at 22:53
  • Unfortunately there seems to be no real solution. Composition is simply not available in email templates when using merge tags or components. – Willem Mulder Dec 10 '18 at 19:04
  • 1
    :( We should probably post an idea, I think it makes a lot of sense to have composition as a great alternative to letter heads – Mattisdada Dec 11 '18 at 5:45
  • If you create it, I'll vote on it :-) – Willem Mulder Dec 11 '18 at 8:37
-1

I can't see in your code apex:insert, apex:define tags. In theory, following the pages below you should able to get what you want:

Using Custom Controllers within Visualforce Email Templates https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm

apex:composition - developer guide page https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_composition.htm

| improve this answer | |
  • 3
    Marcell, could you flesh this out a little bit to clarify how these links answer the question posed above (i.e., the error that Willem described?) – David Reed Dec 18 '19 at 17:44

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.