Salesforce (VisualForce):如何测试“apex:repeat”中是否没有返回记录陈述?

发布于 2024-09-19 13:05:30 字数 2658 浏览 8 评论 0原文

我试图弄清楚如何测试字段(包含在 apex:repeat 中)以查看它们是否为空白或为空,如果是,则在表中显示一些替代文本(例如:没有要显示的记录)而不是空白桌子。下面的现有代码片段:

<apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
   <tr>
    <td>
    <apex:outputField value="{!auditList.Audit_Type__c}" />
    </td>
    <td>
        <apex:outputField value="{!auditList.Delivery_Date__c}" />
    </td>
    <td>
    <apex:outputField value="{!auditList.Review_Date__c}" />
    </td>
   </tr>
</apex:repeat>

因此,在伪代码中,我正在寻找诸如以下的测试:

IF RELATED RECORDS FOUND FOR APEX:REPEAT PERFORM FOLLOWING:

<apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
   <tr>
    <td>
    <apex:outputField value="{!auditList.Audit_Type__c}" />
    </td>
    <td>
        <apex:outputField value="{!auditList.Delivery_Date__c}" />
    </td>
    <td>
    <apex:outputField value="{!auditList.Review_Date__c}" />
    </td>
   </tr>
</apex:repeat>

ELSE IF NO RELATED RECORDS PERFORM FOLLOWING:

<tr>
    <td>
    No records to display.

    </td>
</tr>

提前感谢您的帮助!


更新以响应“eyescream”的第一个答案


尝试了 apex:pageBlock 方法,但在尝试保存/部署时遇到以下错误:

结果:失败问题:<消息传递:电子邮件模板>不能包含

现在,这是一个生成附加 PDF 的电子邮件模板(请参阅下面的代码概要)。那么情况是这样吗……电子邮件模板中不允许使用 pageBlock 吗?感谢您的帮助!

<messaging:emailTemplate subject="Your requested quote #{!relatedTo.Name}" 
recipientType="Contact"
relatedToType="X360_Contract_Cycle__c">

<messaging:plainTextEmailBody >
.
.
.
</messaging:plainTextEmailBody>

<messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">
.
.
.
<apex:pageBlock rendered="{!AND(NOT(ISNULL(auditList)),auditList.size>0)}">

    <apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
       <tr>
        <td>
            <apex:outputField value="{!auditList.Audit_Type__c}" />
        </td>
        <td>
            <apex:outputField value="{!auditList.Delivery_Date__c}" />
        </td>
        <td>
            <apex:outputField value="{!auditList.Review_Date__c}" />
        </td>
       </tr>
   </apex:repeat>

</apex:pageBlock>

<apex:pageBlock rendered="{!OR(ISNULL(auditList),auditList.size=0)}">
    <i>No records to display.</i>
</apex:pageBlock>
.
.
.
</messaging:attachment>
</messaging:emailTemplate>

I am trying to figure out how to test fields (included within a apex:repeat) to see if they are blank, or null, and if so display some alternate text (Ex: No records to display) in the table instead of a blank table. Existing code snippet below:

<apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
   <tr>
    <td>
    <apex:outputField value="{!auditList.Audit_Type__c}" />
    </td>
    <td>
        <apex:outputField value="{!auditList.Delivery_Date__c}" />
    </td>
    <td>
    <apex:outputField value="{!auditList.Review_Date__c}" />
    </td>
   </tr>
</apex:repeat>

So in pseudo code I am looking for a test such as:

IF RELATED RECORDS FOUND FOR APEX:REPEAT PERFORM FOLLOWING:

<apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
   <tr>
    <td>
    <apex:outputField value="{!auditList.Audit_Type__c}" />
    </td>
    <td>
        <apex:outputField value="{!auditList.Delivery_Date__c}" />
    </td>
    <td>
    <apex:outputField value="{!auditList.Review_Date__c}" />
    </td>
   </tr>
</apex:repeat>

ELSE IF NO RELATED RECORDS PERFORM FOLLOWING:

<tr>
    <td>
    No records to display.

    </td>
</tr>

Thanks in advance for the help!


Update in response to first answer from 'eyescream'


Gave the apex:pageBlock method a shot, but ran into the following error when trying to save/deploy:

Result: FAILED Problem: <messaging:emailTemplate> cannot contain <apex:pageBlock>.

Now this is a email template that produces an attached PDF (see general outline of the code below). So is that the case...pageBlock is not allowed within a email template? Thanks for the help!

<messaging:emailTemplate subject="Your requested quote #{!relatedTo.Name}" 
recipientType="Contact"
relatedToType="X360_Contract_Cycle__c">

<messaging:plainTextEmailBody >
.
.
.
</messaging:plainTextEmailBody>

<messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">
.
.
.
<apex:pageBlock rendered="{!AND(NOT(ISNULL(auditList)),auditList.size>0)}">

    <apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
       <tr>
        <td>
            <apex:outputField value="{!auditList.Audit_Type__c}" />
        </td>
        <td>
            <apex:outputField value="{!auditList.Delivery_Date__c}" />
        </td>
        <td>
            <apex:outputField value="{!auditList.Review_Date__c}" />
        </td>
       </tr>
   </apex:repeat>

</apex:pageBlock>

<apex:pageBlock rendered="{!OR(ISNULL(auditList),auditList.size=0)}">
    <i>No records to display.</i>
</apex:pageBlock>
.
.
.
</messaging:attachment>
</messaging:emailTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔纱相望 2024-09-26 13:05:30

一般来说 - 将代码包装在较高的页面元素中(例如 ),然后使用属性 rendered。它是可选的,并且在大多数页面元素上都可用,组件参考应该为您提供完整的信息每个标签支持的属性列表。

在你的情况下,我想类似的事情应该可以解决问题:

<apex:pageBlock rendered="{!AND(NOT(ISNULL(auditList)),auditList.size>0)}">
    Stuff is in, put "repeat" tag here.
</apex:pageBlock>
<apex:pageBlock rendered="{!OR(ISNULL(auditList),auditList.size=0)}">
    No records to display.
</apex:pageBlock>

随意尝试语法。我在公式编辑器中使用了函数名称(用于公式字段、验证规则等),但使用了普通逻辑运算符,例如 &&、||应该也可用。

Generally speaking - wrap your code in higher page element (like <apex:pageBlock>) and then use the attribute rendered. It's optional and available on most of the page elements, the component reference should give you complete list of attributes supported for each tag.

In your case I suppose something like that should do the trick:

<apex:pageBlock rendered="{!AND(NOT(ISNULL(auditList)),auditList.size>0)}">
    Stuff is in, put "repeat" tag here.
</apex:pageBlock>
<apex:pageBlock rendered="{!OR(ISNULL(auditList),auditList.size=0)}">
    No records to display.
</apex:pageBlock>

Feel free to experiment with the syntax. I've used the function names as in the formula editor (for formula fields, validation rules etc.) but normal logic operators like &&, || should be available too.

多情癖 2024-09-26 13:05:30

使用包装器(这是我个人最喜欢的),并使用一个公式来检查渲染属性列表的大小。

<apex:outputPanel rendered="{!relatedTo.Site_Audit__r.size = 0}">
  No Records
</apex:outputPanel>
<apex:outputPanel rendered="{!relatedTo.Site_Audit__r.size != 0}">
  <apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
    ...
  </apex:repeat>
</apex:outputPanel>

Use a wrapper ( is my personal favorite), and use a formula that checks the size of the list for the rendered attribute.

<apex:outputPanel rendered="{!relatedTo.Site_Audit__r.size = 0}">
  No Records
</apex:outputPanel>
<apex:outputPanel rendered="{!relatedTo.Site_Audit__r.size != 0}">
  <apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
    ...
  </apex:repeat>
</apex:outputPanel>
愁以何悠 2024-09-26 13:05:30

坚持使用包装器(使用 apex:outputPanel 或 apex:variable)并创建一个返回列表大小的方法,即

public Integer listSize{get {
 if(auditList != null)
  return auditList.size();
else
  return 0;}}

在确定可见性的条件中使用它。

Stick with the wrapper (use apex:outputPanel or apex:variable) and create a method that returns the list size i.e.

public Integer listSize{get {
 if(auditList != null)
  return auditList.size();
else
  return 0;}}

Use this in the conditional that determines visibility.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文