【问题标题】:Pass ID of current record to Apex Controller将当前记录的 ID 传递给 Apex 控制器
【发布时间】:2019-12-27 15:35:12
【问题描述】:

我正在处理一个 Visualforce 电子邮件模板,该模板将从 Salesforce 中的父贷款 (LLC_BI__Loan__c) 记录发送,并且我正在尝试包含来自子实体参与 (LLC_BI__Legal_Entities__c) 记录的字段。

我无法通过正确的父(贷款)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}" />

【问题讨论】:

    标签: email templates visualforce


    【解决方案1】:

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

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

    <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>
    

    这应该可以帮助您入门。您的关系名称可能是LLC_BI__Legal_Entities__r。您可以在纯 Visualforce 中使用它,限制列表,应用自定义样式,甚至通过不显示它们来过滤某些行(不是最佳性能,但它是一个电子邮件模板,您将多久使用一次。阅读 @987654324 @ 和 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>
    

    对于我的数据,现在只返回 2 个 opps。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多