NullPointerException occurs during rendering Visualforce Email Template when Visualforce Email Template refers to relationship fields of relatedTo obj
Last updated 2019-09-07 ·Reference W-5420212 ·Reported By 14 users
Summary
NullPointerException occurs during rendering Visualforce Email Template when Visualforce Email Template refers to relationship fields of relatedTo object.
Repro
1. Create the following Visualforce email template
Name : TestEtemplate
---
<messaging:emailTemplate subject="Test:{!relatedTo.Name}" recipientType="User" relatedToType="Account">
<messaging:plainTextEmailBody >
{!relatedTo.Name}!!
<apex:repeat value="{!relatedTo.Contacts}" var="cnt">
{!cnt.FirstName} {!cnt.LastName}
</apex:repeat>
</messaging:plainTextEmailBody>
</messaging:emailTemplate>
---
2. create an Approval Process for Account and activate it
Name : TestApproval
Entry Criteria : Account: Account Name EQUALS Test
Approval Assignment Email Template: TestEtemplate (was created in Step 1)
Initial Submitters: Account Owner
3. create the following Apex class
---
public class testClass {
public Id acctId { set; get; }
public testClass(ApexPages.StandardController cont) {
SObject obj = cont.getRecord();
if (obj != null) {
acctId = obj.Id;
}
}
public PageReference doTest() {
createApproval(acctId);
return null;
}
private static void createApproval(Id recId) {
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(recId);
Approval.ProcessResult result = Approval.process(req1);
}
}
---
4. create the following Visualforce page
Name : testVFpage
---
<apex:page standardController="Account" extensions="testClass">
<apex:form >
<apex:commandButton value="DOTEST" action="{!doTest}" rendered="{!acctId != null}"/>
</apex:form>
</apex:page>
---
5. run the following Apex code for creating data via Developer Console
---
Account acct = new Account(Name='Test');
insert acct;
List<Contact> cnts = new List<Contact>();
for(Integer i=0; i < 2000; i++) {
Contact cnt = new Contact(FirstName='Test', LastName='User' + i, AccountId=acct.Id);
cnts.add(cnt);
}
insert cnts;
---
6. check the created Account
note id of the Account.
7. access Visaulforce page with Id of Account which was created in Step 5 as follows
http://hostname:port/apex/testVFpage?id={Account Id}
You will see "DOTEST" button.
8. click on "DOTEST" button
You will see the following error occurs.
---
Process failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, java.lang.NullPointerException: []
Error is in expression '{!doTest}' in component <apex:commandButton> in page testVFpage: Class.testClass.createApproval: line 18, column 1
Class.testClass.doTest: line 11, column 1
Workaround
The workaround is to submit Approval Process from async Apex.
For instance, Future method.
---
public class testClass {
public Id acctId { set; get; }
public testClass(ApexPages.StandardController cont) {
SObject obj = cont.getRecord();
if (obj != null) {
acctId = obj.Id;
}
}
public PageReference doTest() {
createApproval(acctId);
return null;
}
@Future
private static void createApproval(Id recId) {
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(recId);
Approval.ProcessResult result = Approval.process(req1);
}
}
Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.