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? – Pranay Jaiswal Dec 26 '18 at 11:26
  • @PranayJaiswal , Yes I have put a debug statement just before the method call and indeed all the three values are set. – Saikat Deshmukh Dec 26 '18 at 12:02
  • Is it a Visualforce template? – David Reed Dec 26 '18 at 12:56
  • I have seen this happen when fields are null...can you populate them and try again? – Pranay Jaiswal Dec 26 '18 at 18:17
  • @DavidReed yes, it is a visualforce template – Saikat Deshmukh Dec 27 '18 at 5:33

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 '19 at 5:03

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.