TransWikia.com

VF Email Template Error - Attempt to de-reference a null object

Salesforce Asked by Marc Rioux on December 14, 2021

Hello everyone I am trying to get my visualforce email template to work this component is used on the Opportunity and Contact Objects. Both email templates are showing the

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.

But it still works fine on the opportunity object but not on the contact object.

Here is my Controller:

public class ClosingActivitiesController2 {
    public list<Closings.ClosingActivity> closings;
    public String contactId {get; set;}
    public String idStr {get; set;}

    public ClosingActivitiesController2() {
        // can't use passed ID here because it doesn't get set by the component until after instantiation
    }

    public list<closings.ClosingActivity> getClosingActivity() {

        Id passedID = idStr ;
        Closings closings = new Closings();
     // return closings.selectClosingActivity(passedID);

        if(passedId.getSobjectType() == Schema.Contact.SObjectType) {
            return closings.selectClosingActivityByContact(passedID);
        } else {
            return closings.selectClosingActivityByOpportunity(passedID);
        }
    }

}

And Here is my VF Component:

<apex:component controller="ClosingActivitiesController2" access="global">
    <style>
        #tbl1 tr:nth-child(2){
            background-color:#f0f8fc;
        }

        #tbl2 tr:nth-child(2){
            background-color:#f0f8fc;
        }
    </style>
    <apex:attribute id="relatedToID" assignTo="{!idStr}" name="relatedToID" type="String" description="Contact or Opportunity SF ID"/>    
    <apex:repeat value="{!ClosingActivity}" var="c">
        <tr>
            <td align="center" valign="top">
                <table id="tbl1" class="templateDataTable" border="0" cellpadding="10" cellspacing="0"
                  width="100%">
                  <tbody>
                    <tr>
                      <th scope="col" class="dataTableHeading" valign="top">
                      {!c.closingOpp.name}
                      </th>
                      <th style="text-align: right;" scope="col" class="dataTableHeading" valign="top"
                        width="25%">
                        </th>
                    </tr>
                  </tbody>
                </table>
                <br/>
                <table id="tbl2" class="templateDataTable" align="right" border="0" cellpadding="0" cellspacing="0"
                  width="90%">
                  <tbody>
                    <tr>
                      <th class="dataTableHeading">Step</th>
                      <th class="dataTableHeading">Status</th>
                    </tr>
                    <apex:repeat value="{!c.closingOpp.actions__r}" var="t">
                    <tr>
                      <td>
                                {!t.Task__c}
                      </td>
                      <td> {!t.Status__c} </td>
                    </tr>
                    </apex:repeat>
                  </tbody>
                </table>
            </td>
        </tr>
    </apex:repeat>
</apex:component>

Email Temaplate:

<messaging:emailTemplate subject="Closings Update" recipientType="Contact" relatedToType="Contact">
<messaging:plainTextEmailBody >
Current Closings

If you are not seeing your list of open closings please contact The Closing Co. 
</messaging:plainTextEmailBody>
<messaging:emailHeader name="agentclosing">
</messaging:emailHeader>
<messaging:htmlEmailBody >
    <style type="text/css">
            /* Client-specific Styles */
            #outlook a{padding:0;} /* Force Outlook to provide a "view in browser" button. */
            body{width:100% !important;} .ReadMsgBody{width:100%;} .ExternalClass{width:100%;} /* Force Hotmail to display emails at full width */
            body{-webkit-text-size-adjust:none;} /* Prevent Webkit platforms from changing default text sizes. */
            /* Reset Styles */
            body{margin:0; padding:0;}
            img{border:0; height:auto; line-height:100%; outline:none; text-decoration:none;}
            table td{border-collapse:collapse;}
            #backgroundTable{height:100% !important; margin:0; padding:0; width:100% !important;}
            /* Template Styles */
            /* ////////// STANDARD STYLING: COMMON PAGE ELEMENTS ////////// */
            /**
            * @tab Page
            * @section background color
            * @tip Set the background color for your email. You may want to choose one that matches your company's branding.
            * @theme page
            */
            body, #backgroundTable{
                /*@editable*/ background-color:#FAFAFA;
            }
            /**
            * @tab Page
            * @section email border
            * @tip Set the border for your email.
            */
            #templateContainer{
                /*@editable*/ border: 1px solid #DDDDDD;
            }
            /**
            * @tab Page
            * @section heading 1
            * @tip Set the styling for all first-level headings in your emails. These should be the largest of your headings.
            * @style heading 1
            */
            h1, .h1{
                /*@editable*/ color:#202020;
                display:block;
                /*@editable*/ font-family:Arial;
                /*@editable*/ font-size:34px;
                /*@editable*/ font-weight:bold;
                /*@editable*/ line-height:100%;
                margin-top:0;
                margin-right:0;
                margin-bottom:10px;
                margin-left:0;
                /*@editable*/ text-align:left;
            }
            /**
            * @tab Page
            * @section heading 2
            * @tip Set the styling for all second-level headings in your emails.
            * @style heading 2
            */
            h2, .h2{
                /*@editable*/ color:#202020;
                display:block;
                /*@editable*/ font-family:Arial;
                /*@editable*/ font-size:30px;
                /*@editable*/ font-weight:bold;
                /*@editable*/ line-height:100%;
                margin-top:0;
                margin-right:0;
                margin-bottom:10px;
                margin-left:0;
                /*@editable*/ text-align:left;
            }
            /**
            * @tab Page
            * @section heading 3
            * @tip Set the styling for all third-level headings in your emails.
            * @style heading 3
            */
            h3, .h3{
                /*@editable*/ color:#202020;
                display:block;
                /*@editable*/ font-family:Arial;
                /*@editable*/ font-size:26px;
                /*@editable*/ font-weight:bold;
                /*@editable*/ line-height:100%;
                margin-top:0;
                margin-right:0;
                margin-bottom:10px;
                margin-left:0;
                /*@editable*/ text-align:left;
            }
            /**
            * @tab Page
            * @section heading 4
            * @tip Set the styling for all fourth-level headings in your emails. These should be the smallest of your headings.
            * @style heading 4
            */
            h4, .h4{
                /*@editable*/ color:#202020;
                display:block;
                /*@editable*/ font-family:Arial;
                /*@editable*/ font-size:22px;
                /*@editable*/ font-weight:bold;
                /*@editable*/ line-height:100%;
                margin-top:0;
                margin-right:0;
                margin-bottom:10px;
                margin-left:0;
                /*@editable*/ text-align:left;
            }
            /* ////////// STANDARD STYLING: HEADER ////////// */
            /**
            * @tab Header
            * @section header style
            * @tip Set the background color and border for your email's header area.
            * @theme header
            */
            #templateHeader{
                /*@editable*/ background-color:#FFFFFF;
                /*@editable*/ border-bottom:0;
            }
            /**
            * @tab Header
            * @section header text
            * @tip Set the styling for your email's header text. Choose a size and color that is easy to read.
            */
            .headerContent{
                /*@editable*/ color:#202020;
                /*@editable*/ font-family:Arial;
                /*@editable*/ font-size:34px;
                /*@editable*/ font-weight:bold;
                /*@editable*/ line-height:100%;
                /*@editable*/ padding:0;
                /*@editable*/ text-align:center;
                /*@editable*/ vertical-align:middle;
            }
            /**
            * @tab Header
            * @section header link
            * @tip Set the styling for your email's header links. Choose a color that helps them stand out from your text.
            */
            .headerContent a:link, .headerContent a:visited, /* Yahoo! Mail Override */ .headerContent a .yshortcuts /* Yahoo! Mail Override */{
                /*@editable*/ color:#336699;
                /*@editable*/ font-weight:normal;
                /*@editable*/ text-decoration:underline;
            }
            #headerImage{
                height:auto;
                max-width:600px !important;
            }
            /* ////////// STANDARD STYLING: MAIN BODY ////////// */
            /**
            * @tab Body
            * @section body style
            * @tip Set the background color for your email's body area.
            */
            #templateContainer, .bodyContent{
                /*@editable*/ background-color:#FFFFFF;
            }
            /**
            * @tab Body
            * @section body text
            * @tip Set the styling for your email's main content text. Choose a size and color that is easy to read.
            * @theme main
            */
            .bodyContent div{
                /*@editable*/ color:#505050;
                /*@editable*/ font-family:Arial;
                /*@editable*/ font-size:14px;
                /*@editable*/ line-height:150%;
                /*@editable*/ text-align:left;
            }
            /**
            * @tab Body
            * @section body link
            * @tip Set the styling for your email's main content links. Choose a color that helps them stand out from your text.
            */
            .bodyContent div a:link, .bodyContent div a:visited, /* Yahoo! Mail Override */ .bodyContent div a .yshortcuts /* Yahoo! Mail Override */{
                /*@editable*/ color:#336699;
                /*@editable*/ font-weight:normal;
                /*@editable*/ text-decoration:underline;
            }
            /**
            * @tab Body
            * @section data table style
            * @tip Set the background color and border for your email's data table.
            */
            .templateDataTable{
                /*@editable*/ background-color:#FFFFFF;
                /*@editable*/ border:1px solid #DDDDDD;
            }
            .templateDataTable tbody tr:nth-child(2n+1) {
              background-color: #eee;
            }      

            /**
            * @tab Body
            * @section data table heading text
            * @tip Set the styling for your email's data table text. Choose a size and color that is easy to read.
            */
            .dataTableHeading{
                /*@editable*/ background-color:#D8E2EA;
                /*@editable*/ color:#336699;
                /*@editable*/ font-family:Helvetica;
                /*@editable*/ font-size:14px;
                /*@editable*/ font-weight:bold;
                /*@editable*/ line-height:150%;
                /*@editable*/ text-align:left;
            }

            /**
            * @tab Body
            * @section data table heading link
            * @tip Set the styling for your email's data table links. Choose a color that helps them stand out from your text.
            */
            .dataTableHeading a:link, .dataTableHeading a:visited, /* Yahoo! Mail Override */ .dataTableHeading a .yshortcuts /* Yahoo! Mail Override */{
                /*@editable*/ color:#FFFFFF;
                /*@editable*/ font-weight:bold;
                /*@editable*/ text-decoration:underline;
            }

            /**
            * @tab Body
            * @section data table text
            * @tip Set the styling for your email's data table text. Choose a size and color that is easy to read.
            */
            .dataTableContent{
                /*@editable*/ border-top:1px solid #DDDDDD;
                /*@editable*/ border-bottom:0;
                /*@editable*/ color:#202020;
                /*@editable*/ font-family:Helvetica;
                /*@editable*/ font-size:12px;
                /*@editable*/ font-weight:bold;
                /*@editable*/ line-height:150%;
                /*@editable*/ text-align:left;
            }

            /**
            * @tab Body
            * @section data table link
            * @tip Set the styling for your email's data table links. Choose a color that helps them stand out from your text.
            */
            .dataTableContent a:link, .dataTableContent a:visited, /* Yahoo! Mail Override */ .dataTableContent a .yshortcuts /* Yahoo! Mail Override */{
                /*@editable*/ color:#202020;
                /*@editable*/ font-weight:bold;
                /*@editable*/ text-decoration:underline;
            }
            /**
            * @tab Body
            * @section button style
            * @tip Set the styling for your email's button. Choose a style that draws attention.
            */
            .templateButton{
                -moz-border-radius:3px;
                -webkit-border-radius:3px;
                /*@editable*/ background-color:#336699;
                /*@editable*/ border:0;
                border-collapse:separate !important;
                border-radius:3px;
            }
            /**
            * @tab Body
            * @section button style
            * @tip Set the styling for your email's button. Choose a style that draws attention.
            */
            .templateButton, .templateButton a:link, .templateButton a:visited, /* Yahoo! Mail Override */ .templateButton a .yshortcuts /* Yahoo! Mail Override */{
                /*@editable*/ color:#FFFFFF;
                /*@editable*/ font-family:Arial;
                /*@editable*/ font-size:15px;
                /*@editable*/ font-weight:bold;
                /*@editable*/ letter-spacing:-.5px;
                /*@editable*/ line-height:100%;
                text-align:center;
                text-decoration:none;
            }
            .bodyContent img{
                display:inline;
                height:auto;
            }
            /* ////////// STANDARD STYLING: FOOTER ////////// */
            /**
            * @tab Footer
            * @section footer style
            * @tip Set the background color and top border for your email's footer area.
            * @theme footer
            */
            #templateFooter{
                /*@editable*/ background-color:#FFFFFF;
                /*@editable*/ border-top:0;
            }
            /**
            * @tab Footer
            * @section footer text
            * @tip Set the styling for your email's footer text. Choose a size and color that is easy to read.
            * @theme footer
            */
            .footerContent div{
                /*@editable*/ color:#707070;
                /*@editable*/ font-family:Arial;
                /*@editable*/ font-size:12px;
                /*@editable*/ line-height:125%;
                /*@editable*/ text-align:center;
            }
            /**
            * @tab Footer
            * @section footer link
            * @tip Set the styling for your email's footer links. Choose a color that helps them stand out from your text.
            */
            .footerContent div a:link, .footerContent div a:visited, /* Yahoo! Mail Override */ .footerContent div a .yshortcuts /* Yahoo! Mail Override */{
                /*@editable*/ color:#336699;
                /*@editable*/ font-weight:normal;
                /*@editable*/ text-decoration:underline;
            }
            .footerContent img{
                display:inline;
            }
            /**
            * @tab Footer
            * @section utility bar style
            * @tip Set the background color and border for your email's footer utility bar.
            * @theme footer
            */
            #utility{
                /*@editable*/ background-color:#FFFFFF;
                /*@editable*/ border:0;
            }
            /**
            * @tab Footer
            * @section utility bar style
            * @tip Set the background color and border for your email's footer utility bar.
            */
            #utility div{
                /*@editable*/ text-align:center;
            }
            #monkeyRewards img{
                max-width:190px;
            }
        </style>

<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="bodyTable">
    <tr>
        <td align="left" valign="top">
            <table border="0" cellpadding="20" cellspacing="0" width="600" id="emailContainer">
                <tr>
                    <td align="center" valign="top">
                        <table border="0" cellpadding="20" cellspacing="0" width="100%" id="emailHeader">
                            <tr>
                                <td align="left" valign="bottom">
                                <h2>Current Closings</h2>
                                </td>
                                <td>
                                    <apex:image id="Logo" value="https://c.na16.content.force.com/servlet/servlet.ImageServer?id=015j00000003JTs&oid=00Dj0000001no0u&lastMod=1421265141000" 
height="60" width="146"/>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td align="center" valign="top">
                        <table class="dataTable" border="0" cellpadding="20" cellspacing="0" width="100%" id="emailBody">
                                    <c:ClosingActivity relatedToID="{!Recipient.Id}"/>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td align="center" valign="top">
                        <table border="0" cellpadding="20" cellspacing="0" width="100%" id="emailFooter">
                            <tr>
                                <td align="center" valign="top">
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

3 Answers

Your general problem has to do with how the Email template editor interacts with you template and included custom component

There are two use cases:

Initial display of the Email template detail page

here, the value of the passed attribute to the component

<c:ClosingActivity relatedToID="{!Recipient.Id}

is null. Hence,in the custom component's controller public String idStr {get; set;} will have a value of null. There is no way around this. You have to code for this use case in the component's controller. As an aside, you should be passing attributes of type ID, not String.

In your case, you need to adapt your method

public list<closings.ClosingActivity> getClosingActivity() {

    Id passedID = idStr ; // idStr is unnecessary, let component set passedId
    Closings closings = new Closings();
    if (passedId == null)
       return new List<Closings.ClosingActivity>();
    else
    if(passedId.getSobjectType() == Schema.Contact.SObjectType) {
        return closings.selectClosingActivityByContact(passedID);
    } else {
        return closings.selectClosingActivityByOpportunity(passedID);
    }
}

Email Template Preview/test

here, the value of the passed attribute to the component

<c:ClosingActivity relatedToID="{!Recipient.Id} 

will have a value because the dialog prompts you to enter a relatedTo and a recipient

Answered by cropredy on December 14, 2021

Please change this line of code in VF template:

From

<c:ClosingActivity relatedToID="{!Recipient.Id}"/>

To

<c:ClosingActivity relatedToID="{!relatedTo.Id}"/>

Answered by iamsfdc on December 14, 2021

Please add a debug statement in your constructor of class and confirm whether the Value is NULL.

System.Debug('ID**** ' + idStr );

Please make sure you pass the value from VF template into Component for attribute called "relatedToID".

Answered by iamsfdc on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP