如何使我的列表扩展返回多个结果?

时间:2013-12-24 22:45:32

标签: visualforce apex

目前,我只能返回一个结果,因为我已准确指定了我想要的广告系列返回此行:

SELECT Id from campaign WHERE Id = '701i0000000JXz5' 

我希望我的页面列出将特定字段设置为特定状态的所有广告系列。当我将上面给出的行更改为:

SELECT Id from campaign WHERE Type = 'Direct Mail'

我收到以下错误:

System.QueryException: List has more than 1 row for assignment to SObject

我做过的事情是将我的列表限制为一个项目,但我不确定它是什么。

这是视觉力页面:

<apex:page standardController="campaign" recordSetVar="dmcampaigns" extensions="DirectMailfilter">  <apex:form >
     <apex:pageBlock title="Direct Mail">
             <apex:pageBlockTable value="{!dmcampaigns}" var="s">
                  <apex:column value="{!s.StartDate}"/>
                  <apex:column value="{!s.Type}"/>  
                  <apex:column value="{!s.status}"/>     
                  <apex:column value="{!s.Name}"/>
             </apex:pageBlockTable> 
     </apex:pageBlock> </apex:form> </apex:page>

使用此扩展程序:

public class DirectMailfilter {

public id camRecId;

public DirectMailfilter(ApexPages.StandardsetController controller) {

camRecId = [SELECT Id from campaign WHERE Id = '701i0000000JXz5'].id;

}

List<campaign> dmcampaigns;

public List<campaign> getdmcampaigns() {
    if(camRecId != null) {
    dmcampaigns= [SELECT StartDate, Type, Status, Name 
                    FROM Campaign 
                    WHERE campaign.id=:camRecId];
    }
return dmcampaigns;
}  
}

1 个答案:

答案 0 :(得分:0)

根据错误,我认为您已将新过滤器应用于第一个查询(camRecId)。但是下面会查询您的“广告系列列表”!

你真的不需要那个查询,它可以简化为这样的东西:

public class DirectMailfilter {
    public List<campaign> dmcampaigns {get;private set;}

    public DirectMailfilter(ApexPages.StandardsetController controller) {
        dmcampaigns = [SELECT StartDate, Type, Status, Name 
                    FROM Campaign 
                    WHERE Type = 'Direct Mail'];
    }
}

查询将只运行一次(当构造函数在第一页加载时运行),而在旧代码中,每次调用getter时都会发送一个查询(例如,每次点击某些表单提交按钮时)。

您可以阅读有关getter / setters here

的更多信息