将当前记录的 ID 传递给 Apex 控制器

Pass ID of current record to Apex Controller

我正在处理一个 Visualforce 电子邮件模板,该模板将从 Salesforce 中的父贷款 (LLC_BI__Loan__c) 记录发送,我正在尝试包含子实体参与 ([=26) =]) 条记录。

我无法通过正确的父(贷款)ID 来获取正确的子记录。谁能看出我可能哪里出错了?

提前致谢

组件:(名称 = 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>

控制器:(名称 = 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); 

 }

     }

         }

电子邮件模板:

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

我们没有您的对象(无论 "LLC_BI" 托管包是什么),但这应该有所帮助。

如果它只是一个普通的旧(直接)相关列表 - 您不需要组件和查询。相关列表直接在 VF 电子邮件模板中可用,您只需要准确知道关系的名称即可。以下是客户和机会的示例:

<messaging:emailTemplate subject="" 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>

这应该可以帮助您入门。您的关系名称可能是 LLC_BI__Legal_Entities__r。您可以在纯 Visualforce 中使用它走得更远,限制列表,应用自定义样式,甚至通过不显示它们来过滤某些行(不是最好的性能明智,但它是一个电子邮件模板,您将多久使用一次。阅读有关 <apex:variable>rendered 属性,如果你好奇的话)。

但如果你真的需要查询(需要 WHERE 子句、ORDER BY 等)——你需要控制器、组件和最终模板。

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>

对于我的数据,现在 returns 只有 2 个机会。