4

I have apex code that sends out emails using templates. The code sets the RelatedTo id as a contact, and the WhatID as a custom object which is a child of contact. Sometimes the child object is a child of the RelatedTo object, and sometimes it's not.

When I test the merge fields for the email template, it works correctly, but when the email actually gets sent out, if there is no child object for the related to object, the merge fields are null. I really need the merge fields to be pulled from the child object (which is referenced by the WhatID), even if it is a child of a different contact.

Is there any way for me to accomplish this?

4
  • you may need to use visualforce email template.
    – Ratan Paul
    Feb 14, 2016 at 10:47
  • @Ratan: and then select the relevant info in soql?
    – hgolov
    Feb 14, 2016 at 10:56
  • Yes correct. You can use controller and based on that get all childs
    – Ratan Paul
    Feb 14, 2016 at 10:57
  • Ok - please add this as an answer and I'll mark it correct.
    – hgolov
    Feb 14, 2016 at 10:57

1 Answer 1

2

SO you could use Visualforce email template.

In Visualforce email template use VF component and controller.

In controller based RelatedTo get all childs and show then in VF controller.

Use this VF component in Visualforce email template.

Example: http://www.infallibletechie.com/2013/05/visualforce-email-template-with-custom.html

2
  • How do I access the whatId from the template?
    – hgolov
    Feb 14, 2016 at 19:20
  • @hgolov you can pass the whatId from email template to component using apex variable check above mentioned example
    – Ratan Paul
    Feb 15, 2016 at 2:22

You must log in to answer this question.

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