+ Start a Discussion
aveforstudy1.3196212890020557Eaveforstudy1.3196212890020557E 

Error while using Custom component in a visualforce email template

Hi,

I am in need of some urgent help. I am using a Visual force Email template and i need to get some values in it by using a query therefore i have created a custom component and adding it in the email template. But i am getting an error "Error occurred trying to load the template for preview: Attempt to de-reference a null object. "  i Dont know what i am missing. Any help will be of great use here.

Follwing is the email template,custom component and its controller

 

Email Template

<messaging:emailTemplate subject="Account Budget Approval Request for {!relatedTo.Account__r.Name} - {!relatedTo.Year__c}" recipientType="User" relatedToType="Budget__c" >
<messaging:htmlEmailBody >
 <style>
        body{
            font-size: 12px;
            font-family: Arial;
        }
        div{
            font-family: arial;
            font-size: 12px;
            color: #000000;
        }
        .label{
            font-size: 12px;
            font-weight: bold;
            direction: rtl;
            padding-right: 10px;
            width: 120px;
            font-family: Arial;
        }
        .data{
            font-size: 12px;
            font-family: Arial;
        }
        
        .detailsheader{
            background-color: #E5ECF9;
            font-weight: bold;
            width: 90%;
            height: 20px;
            margin-top: 10px;
            margin-bottom: 10px;
            padding-top: 5px;
            padding-left: 5px;
            vertical-align: middle;
            font-family: Arial;
        }
            
    </style>

    <apex:outputPanel layout="block" style="font-family: Arial; margin-bottom: 20px;">
     Hi {!relatedTo.OwnerManagerName__r.Name} ,  
    </apex:outputPanel>
    <apex:outputPanel layout="block" style="font-family: Arial; margin-bottom: 20px;">
      Please review the following account budget and take necessary action.
    </apex:outputPanel>
    <apex:outputPanel layout="block" styleClass="detailsheader">
            Account Budget
    </apex:outputPanel>
    
    <apex:panelGrid columns="4" columnClasses="label,data" >
       <apex:outputLabel value="Budget Id"/>
        <c:SalesforceURL relativeurl="{!relatedTo.id}" displayname="{!relatedTo.Name}"/>
        <apex:outputLabel value="Total Budget"/>
        <apex:outputText value="{!relatedTo.Total_Budget__c}"/>
        <apex:outputLabel value="Account Name"/>
        <apex:outputText value="{!relatedTo.Account__r.Name}"/>
        <apex:outputLabel value="Account Manager"/>
        <apex:outputText value="{!relatedTo.Owner.Name}"/>
        <apex:outputLabel value="Year"/>
        <apex:outputText value="{!relatedTo.Year__c}"/>
         <apex:outputLabel value="Created Date"/>
        <apex:outputText value="{!relatedTo.CreatedDate}"/>
        <apex:outputLabel value="Sales Region"/>
        <apex:outputText value="{!relatedTo.SalesRegion__c}"/>
        <apex:outputLabel value="Last Action By"/>
        <apex:outputText value="{!relatedTo.LastModifiedBy.Name}"/>
        
    </apex:panelGrid>
    <apex:outputPanel layout="block" styleClass="detailsheader">
            Monthly Budget Information
    </apex:outputPanel>
    <c:MonthlyBudgetValues Budget_Id="{!relatedTo.id}"></c:MonthlyBudgetValues>
    <apex:outputPanel layout="block" style="font-style:italic; color: gray; margin-top: 20px; font-size:10px">
            This is a system generated email.
    </apex:outputPanel>
   
     
</messaging:htmlEmailBody>
</messaging:emailTemplate>

 

component

<apex:component controller="BudgetControllerForEmail" access="global">
<apex:attribute name="Budget_Id" type="String" description="Budget Id" assignTo="{!Budget}" access="global"/>
<apex:dataTable value="{!MonthlyBudgetValues}" var="MB">
             <apex:column headerValue="Station">
                        <apex:outputText value="{!MB.selectedStn}"/>
             </apex:column>
                    <apex:column headerValue="Jan">
                        <apex:outputText value="{!MB.Jan}"/>
                    </apex:column>
                    <apex:column headerValue="Feb">
                        <apex:outputText value="{!MB.Feb}"/>
                    </apex:column>
                    <apex:column headerValue="Mar">
                        <apex:outputText value="{!MB.Mar}"/>
                    </apex:column>
                    <apex:column headerValue="Apr">
                        <apex:outputText value="{!MB.Apr}"/>
                    </apex:column>
                    <apex:column headerValue="May">
                        <apex:outputText value="{!MB.May}" />
                    </apex:column>
                    <apex:column headerValue="Jun">
                        <apex:outputText value="{!MB.Jun}"/>
                    </apex:column>
                    <apex:column headerValue="Jul">
                        <apex:outputText value="{!MB.Jul}"/>
                    </apex:column>
                    <apex:column headerValue="Aug">
                        <apex:outputText value="{!MB.Aug}"/>
                    </apex:column>
                    <apex:column headerValue="Sept">
                        <apex:outputText value="{!MB.Sept}"/>
                    </apex:column>
                    <apex:column headerValue="Oct">
                        <apex:outputText value="{!MB.Oct}"/>
                    </apex:column>
                    <apex:column headerValue="Nov">
                        <apex:outputText value="{!MB.Nov}"/>
                    </apex:column>
                    <apex:column headerValue="Dec">
                        <apex:outputText value="{!MB.Dec}"/>
                    </apex:column>
                    </apex:dataTable>

                    
</apex:component>

 

controller

public without sharing class BudgetControllerForEmail {
    public String Budget{get;set;}
    
    private final List<budgetMonth> toDisplay;
    List<Monthly_Budget__c> ExistingMonthlyBudget = new List<Monthly_Budget__c>();
    public BudgetControllerForEmail() {
    System.debug('inside constructor');
    }
    public List<budgetMonth> getMonthlyBudgetValues(){
        //if(budget.Id!=null){
                System.debug('Budget @123'+Budget);
                ExistingMonthlyBudget =[Select m.Id,m.Name, m.Month__c, m.Budget__c, m.Budget_Amount__c,m.Budget__r.Account__c,m.Budget__r.Year__c,m.Budget__r.Name,m.Station__c,m.YMSA__c From Monthly_Budget__c m where m.Budget__c='a05K0000000b0Aj'];
                Set<String> uniqueBudgetName = new Set<String>();
                for(Monthly_Budget__c b:ExistingMonthlyBudget){
                budgetMonth mBudget = new budgetMonth();
                if(!uniqueBudgetName.contains(b.Station__c)){
                                uniqueBudgetName.add(b.Station__c);
                                mBudget.selectedStn = b.Station__c;
                                        for(Monthly_Budget__c budget:ExistingMonthlyBudget){
                                        if(budget.Station__c == mBudget.selectedStn){
                                        if(budget.Month__c=='Jan'){
                                                        mBudget.Jan = (Integer)budget.Budget_Amount__c;
                                                mBudget.YMSAJan = budget.YMSA__c;
                                        }
                                                if(budget.Month__c=='Feb'){
                                                        mBudget.Feb = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSAFeb = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Mar'){
                                                        mBudget.Mar = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSAMar = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Apr'){
                                                        mBudget.Apr = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSAApr = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='May'){
                                                        mBudget.May = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSAMay = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Jun'){
                                                        mBudget.Jun = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSAJun = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Jul'){
                                                        mBudget.Jul = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSAJul = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Aug'){
                                                        mBudget.Aug = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSAAug = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Sept'){
                                                        mBudget.Sept = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSASept = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Oct'){
                                                        mBudget.Oct = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSAOct = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Nov'){
                                                        mBudget.Nov = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSANov = budget.YMSA__c;
                                                }
                                                if(budget.Month__c=='Dec'){
                                                        mBudget.Dec = (Integer)budget.Budget_Amount__c;
                                                        mBudget.YMSADec = budget.YMSA__c;
                                                }       
                                }       
                        }
                        toDisplay.add(mBudget);
                }
            }       
       // }
        return toDisplay;
    }
public class budgetMonth{
    Public String budgetId{get;set;}
    Public String monthlyBudgetId{get;set;}
    Public String YMSAJan{get;set;}
    Public String YMSAFeb{get;set;}
    Public String YMSAMar{get;set;}
    Public String YMSAApr{get;set;}
    Public String YMSAMay{get;set;}
    Public String YMSAJun{get;set;}
    Public String YMSAJul{get;set;}
    Public String YMSAAug{get;set;}
    Public String YMSASept{get;set;}
    Public String YMSAOct{get;set;}
    Public String YMSANov{get;set;}
    Public String YMSADec{get;set;}
    Public String Year{get;set;}
    Public Integer Jan{get;set;}
    Public Integer Feb{get;set;}
    Public Integer Mar{get;set;}
    Public Integer Apr{get;set;}
    Public Integer May{get;set;}
    Public Integer Jun{get;set;}
    Public Integer Jul{get;set;}
    Public Integer Aug{get;set;}
    Public Integer Sept{get;set;}
    Public Integer Oct{get;set;}
    Public Integer Nov{get;set;}
    Public Integer Dec{get;set;}
    Public String selectedStn{get;set;}
    Public String disabledField{get;set;}
}
}

 

 

Thanks