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?

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

  • How do I access the whatId from the template? – hgolov Feb 14 '16 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 '16 at 2:22

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.