Visualforce / Apex:使用一个控制器访问两个对象

Visualforce / Apex: Access two objects with one controller

我遇到了一个问题,我必须设置一个呈现为 pdf 的 visualforce 页面,它会输出自动生成的发票。由于实习生工作流程,此页面必须设置为机会对象。

使用的大部分字段取自机会对象本身并且工作正常。

但我还需要访问 OpportunityLineItem 字段以在发票上显示产品。

如何实现?我是否必须在 apex 中编写控制器扩展,或者是否可以不编写?

作为替代方案,这最终是否可以通过从 Opportunity 引用到 OpportunityLineItem 的交叉公式字段实现?我尝试了这个,但在机会对象的公式字段中找不到 select OpportunityLineItem 的任何可能性。

非常感谢任何帮助。谢谢!!

下面是一个示例页面,它使用根据 this 文档参考修改的标准控制器访问给定机会的 OpportunityLineItems。

<apex:page standardController="Opportunity">

<table border="0" >

    <tr>

        <th>Description</th><th>Quantity</th>

        <th>Unit Price</th><th>Name</th>

    </tr>

    <apex:repeat var="oli" value="{!Opportunity.OpportunityLineItems}">

    <tr>

        <td>{!oli.Description}</td>

        <td>{!oli.quantity}</td>

        <td>{!oli.unitprice}</td>

        <td>{!oli.Name}</td>

    </tr>

    </apex:repeat> 

</table>

</apex:page>

关于公式字段,您无法访问 parent 公式中的 child 字段,原因很简单,因为它是一对多关系。 parent Opportunity 不知道要查找 children 中的哪一个。

您能做的最好的事情就是制作一个常规(文本或其他)字段,运行 一个由 parent(机会)上的相关字段更改触发的流程构建器并触发 Flow 循环遍历 children (LineItems) 并根据您指定的某些条件对 parent 进行更改。