I am trying to render an email using the method: Messaging.renderemailtemplate(), by passing three parameters: templateId,whoId ( user ) ,whatId ( Custom Object ) from a Trigger.

The insertion of the custom record and calling the method: renderemailtemplate() happens in the same transaction, but I get the error:

System.EmailTemplateRenderException: UNKNOWN_EXCEPTION: java.lang.NullPointerException


public static void emailMethodName(Id templateId, sObject who, sObject what)
Messaging.SingleEmailMessage email = 
Messaging.renderStoredEmailTemplate(templateId,(Id)who.get('Id'), (Id)what.get('Id')); // I get the error in this line.

This method is called from a trigger handler Class in the after update context.

Any idea why this is happening ?

  • can you check all the fields referenced in the template are populated for that record? Dec 26, 2018 at 11:26
  • @PranayJaiswal , Yes I have put a debug statement just before the method call and indeed all the three values are set. Dec 26, 2018 at 12:02
  • Is it a Visualforce template?
    – David Reed
    Dec 26, 2018 at 12:56
  • I have seen this happen when fields are null...can you populate them and try again? Dec 26, 2018 at 18:17
  • @DavidReed yes, it is a visualforce template Dec 27, 2018 at 5:33

1 Answer 1


After having extensive discussion with Salesforce.com, the conclusion is that the method: Messaging.renderemailtemplate() rechecks, if the running user have at least Read Permission to the object being sent as a parameter. The error mentioned above is shown if the permission is not / cannot be configured.

  • Yes, I learned this myself as well, and this is a huge oversight on SF’s part!
    – number41
    Dec 21, 2019 at 5:03

You must log in to answer this question.

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