function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MrBrianMrBrian 

VisualForce Component in VisualForce Email Template De-referencing Null Object?

Hi there, beginner VF/Apex coder here.

I have a VF email template where the relatedTo object is Asset. I would like to add a VF component that displays a table of all the sibling assets under the same account. Here is my code so far:

Constructor:
public class findSiblingAssets {
    private final List<Asset> assets;

    public Asset ast {get; set;}
    
    public findSiblingAssets() {
        
        assets = [select Id, AccountId, Name, UsageEndDate from Asset where AccountId = :ast.AccountId];
    }

    public List<Asset> getSiblingAssets() {
        return assets;
    }
}
Component (Named "FindSiblingAssets"):
<apex:component controller="findSiblingAssets" access="global">
    <apex:attribute type="Asset" name="originast" assignTo="{!ast}"/>
    <apex:dataTable value="{!SiblingAssets}" var="sib">
        <apex:column >
            <apex:facet name="header">Asset Name</apex:facet>
            {!sib.Name}
        </apex:column>
    </apex:dataTable>

</apex:component>

And here are relevant parts of the email template:
 
<messaging:emailTemplate relatedToType="Asset"
  subject="Subject Line here"
  replyTo="replyaddress@email.com">
  <messaging:htmlEmailBody >
  <html>
...
<c:FindSiblingAssets originast="{!relatedTo}"/>
...
</html>
</messaging:htmlEmailBody> 
</messaging:emailTemplate>


However, after I save the Email Template page, or try to to a test merge, I get the message: "Error occurred trying to load the template for preview: Attempt to de-reference a null object. Please try editing your markup to correct the problem."

I cannot figure out for the life of me what I'm doing wrong! Please help!
Alex SelwynAlex Selwyn
Do you see any problem in sending preview or actual email? 

This happens since you are passing in an object (originast="{!relatedTo}"), which will be null in the preview.

I would not worry, unless you have specific reason to see the preview in the email template.

To expose a preview to the end user, I had used the component in a VF page where in I would pass the 'originast="{!relatedTo}"' from the VF controller.
MrBrianMrBrian
It would not let me send any email, and it would not run based off of any WFR, etc.

I did modify the code so that rather than passing the Asset, it should now just pass the {!relatedTo.AccountId} String of the asset. However, it seems that this value is not making it to the controller; (if I remove the "where" criteria, the table displays correctly, however otherwise, no rows are returned.)

Here is the updated code, please help!

Controller:
public class findSiblingAssets {
    private final List<Asset> assets;

    public String astAcctId {get; set;}
        
    public findSiblingAssets() {
     
        assets = [select Id, AccountId, Name, UsageEndDate from Asset where AccountId = :astAcctId ];
    }

    public List<Asset> getSiblingAssets() {
        return assets;
    }
}
VF Component:
<apex:component controller="findSiblingAssets" access="global">
    <apex:attribute type="String" name="originast" assignTo="{!astAcctId}" description="the relatedTo Asset Account Id"/>
    <apex:dataTable value="{!SiblingAssets}" var="sib">
        <apex:column >
            <apex:facet name="header">Asset Name</apex:facet>
            {!sib.Name}
        </apex:column>
    </apex:dataTable>

</apex:component>
VF email template component call:
<c:FindSiblingAssets originast="{!relatedTo.AccountId}"/>
Ashish TakkeAshish Takke
Hi,
Did anyone find solution for this? I am also facing same issue.Somehow {!related.AccountId} is not reaching to VF Component controller, where clause with static value, returning back result.
Please help if anyone came across this issue. Your help really appreachiated.