質問

Currently, I'm only able to return one result because I have specified exactly what campaign I want returned this this line:

SELECT Id from campaign WHERE Id = '701i0000000JXz5' 

I want my page to list all campaigns that have a specific field set to a specific status. When I change the line given above to something like:

SELECT Id from campaign WHERE Type = 'Direct Mail'

I get the following error:

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

Something I've done is limiting my list to one item, but I'm not sure what it is.

Here is the visual force page:

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

with this extension:

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;
}  
}
役に立ちましたか?

解決

Based on the error I think you've been applying the new filter to the 1st query (camRecId). But your "list of campaigns" is queried for below!

You don't really need that query, it can be simplified to something like this:

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'];
    }
}

The query will be run only once (when the constructor runs on first page load) whereas in your old code you would send a query every time getter is called (for example every time you hit some form submission buttons).

You can read more about the getters/setters here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top