1

I have created a visualforce email template and I would like to display rows from a related object ORC__c which belongs to the Account object.

The email template would be selected like so Contacts > Joe Bloggs (contact) > Send Email > Add related to Account (field) > Select Template.

I have found that no rows are returned using the snippets I have created below. I have tried to debug the function by using a static ID on the SOQL statement, this does return rows in the template. I have also tried running the SOQL query with a static account ID in the developer console which also worked.

Email template:

<messaging:emailTemplate subject="My email subject" recipientType="Contact" relatedToType="Account">
<messaging:htmlEmailBody >

<p><apex:outputText value="{!TODAY()}" /></p>

<p>REF: <apex:outputText value="{!relatedTo.Name}" /></p>

<p>Dear {!relatedTo.Contact.Name},</p>

<c:ORCLetter_Component acctId="{!relatedTo.Id}"/>

Component:

<apex:component controller="findORCInstances" access="Global" >
<apex:attribute name="AcctId" type="Id" description="Id of the account" assignTo="{!accountId}"/>

<apex:dataTable value="{!ORCInstances}" var="wlan" styleClass="orcTableList">
    <apex:facet name="header">Wireless LANs</apex:facet>
    <apex:column>
        <apex:facet name="header">SSID Name</apex:facet>
        {!wlan.SSID__c}
    </apex:column>
</apex:dataTable>

</apex:component>

Controller:

public class findORCInstances {
    private final List<ORC__c> wlans;
    public Id accountId {get;set;}

    public findORCInstances() {
        wlans = [select Name, SSID__c, NumAPs__c, Percentage_Utilisation__c from ORC__c where Client__c = ':accountId'];
    }

    public List<ORC__c> getORCInstances() {
        return wlans;
    }

    public Id getActId() {
        return accountId;
    }
}

Note: I use client__c as an alias for Account

1 Answer 1

1

The issue is that you are accessing the attribute in the constructor of the component. At the time of construction the attribute has not yet been set, so does not have a value and is null.

You need to make the query outside of the constructor. For example, you could change your code to make the query as part of the getORCInstances (note you have to change wlans to not be final).

public List<ORC__c getORCInstances() {
    if (wlans == null) {
        wlans = [select Name, SSID__c, NumAPs__c, Percentage_Utilisation__c from ORC__c where Client__c = :accountId];
    }
    return wlans;
}

There's more detailed information about execution order in the documentation in Order of Execution for Visualforce Page Get Requests.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .