Search code examples
emailtemplatesvisualforce

Pass ID of current record to Apex Controller


I'm working on a Visualforce Email Template which will be sent from the parent Loan (LLC_BI__Loan__c) record in Salesforce, and I'm trying to include fields from the child Entity Involvement (LLC_BI__Legal_Entities__c) record(s).

I'm unable to pass the correct parent (Loan) Id to get the correct child records. Can anyone see where I may be going wrong ?

Thank you in advance

Component:(Name = BorrowerRecordsFromLoans)

<apex:component controller="BorrowersOnLoans" access="global">

    <apex:attribute name="currentRecordId" description="" assignTo="{!loanId}" type="Id"/>

        <apex:dataTable value="{!relatedBorrowers}" var="borrower">

            <apex:column >

                    <apex:facet name="header">Borrower Name</apex:facet>

                {!borrower.LLC_BI__Borrower_Type__c}

            </apex:column>

        </apex:dataTable>

</apex:component>

Controller: (Name = BorrowersOnLoans)

    public class BorrowersOnLoans { 

    public Id loanId { get; set { loanId = value; loadChildren(); } } 

    public LLC_BI__Legal_Entities__c[] relatedBorrowers { get; set; } 

    void loadChildren() 

{ 

List <LLC_BI__Legal_Entities__c> entList = new List<LLC_BI__Legal_Entities__c>(); 

for(LLC_BI__Loan__c loan: 

[SELECT Id, (SELECT Entity_Name__c FROM LLC_BI__Legal_Entities__r ORDER BY Borrower_Number__c) FROM LLC_BI__Loan__c WHERE Id = :loanId])

 {

 for(LLC_BI__Legal_Entities__c ent:loan.LLC_BI__Legal_Entities__r) entList.add(ent); 

 }

     }

         }

Email Template:

<c:BorrowerRecordsFromLoans currentRecordId="{!relatedTo.Id}" />

Solution

  • We don't have your objects (whatever "LLC_BI" managed package is) but this should help.

    If it's just a plain old (directly) related list - you don't need a component & query. The related list is directly available in the VF email template, you just need to know exactly the relationship's name. Here's example with Account and Opportunities:

    <messaging:emailTemplate subject="https://stackoverflow.com/q/59502890/313628" recipientType="User" relatedToType="Account">
    <messaging:htmlEmailBody >
    {!relatedTo.AccountNumber}<br/>
    {!relatedTo.Name}<br/>
    {!relatedTo.BillingCity}<br/>
    
    Opportunities, like that:
    <ol>
        <apex:repeat value="{!relatedTo.Opportunities}" var="o">
            <li>{!o.Name}, {!o.StageName}, {!o.Amount}</li>
        </apex:repeat>
    </ol>
    
    <hr/>
    or like that:
    
    <apex:dataTable value="{!relatedTo.Opportunities}" var="o">
        <apex:column value="{!o.Name}" />
        <apex:column value="{!o.StageName}" />
        <apex:column value="{!o.Amount}" />
    </apex:dataTable>
    </messaging:htmlEmailBody>
    </messaging:emailTemplate>
    

    This should get you started. Your relationship name will probably be LLC_BI__Legal_Entities__r. You can go pretty far with it in pure Visualforce, limit the list, apply custom styling, even filter some rows by not displaying them (not best performance wise but it's an email template, how often you'll use it. Read about <apex:variable> and rendered attribute if you're curious).

    But if you really need a query (need WHERE clause, ORDER BY etc) - you need controller, component and the final template.

    public with sharing class Stack59502890 {
        public Id accountId {get; set;}
    
        public List<Opportunity> getWonOpportunities(){
            return [SELECT Name, StageName, Amount
                FROM Opportunity
                WHERE AccountId = :accountId AND IsWon = true
                ORDER BY Name];
        }
    }
    
    <apex:component access="global" controller="Stack59502890">
        <apex:attribute name="currentRecordId" description="" assignTo="{!accountId}" type="Id"/>
        <apex:repeat value="{!wonOpportunities}" var="o">
            <li>{!o.Name}, {!o.StageName}, {!o.Amount}</li>
        </apex:repeat>
    </apex:component>
    
    <messaging:emailTemplate subject="Stack59502890" recipientType="User" relatedToType="Account">
    <messaging:htmlEmailBody >
        <c:Stack59502890 currentRecordId="{!relatedTo.Id}" />
    </messaging:htmlEmailBody>
    </messaging:emailTemplate>
    

    For my data this now returns only 2 opps.