function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jordan@BracketLabsJordan@BracketLabs 

ORDER BY in Apex.StandardSetController

We have an Apex class that uses the 'getQueryLocator' to return a single page of results, 2000 records (single page max).

 

We are trying to implement:

    public ApexPages.StandardSetController getCampaignsSsc() {
        if (this.campaigns == null) {
            this.campaigns = new ApexPages.StandardSetController(
                Database.getQueryLocator([SELECT Id, Name, StartDate, EndDate, Type, Status, Description, Owner.Name, NumberOfLeads, NumberOfContacts, NumberOfOpportunities, NumberOfWonOpportunities, AmountAllOpportunities, Exclude_from_Calendar__c FROM Campaign ORDER BY StartDate DESC])
            );
            if(this.pagesize == null){
                this.pagesize = 2000;
            }
            this.campaigns.setPageSize(this.pagesize);
        return campaigns;
    }

 However, the campaign records always seem to come back sorted by Name is it possible I'm not setting something with the SSC before running the 'getRecords' operation that is causing them to always return this way (reguardless of the arguement in the ORDER BY clause)?

 

p.s. sorting the records on the server before returning them to the page isn't really an option as with 2000 records it would overflow the script statements limits.

Jordan@BracketLabsJordan@BracketLabs

Found the answer:

 

When one applies the 'FilterId' using the 'setFilterId' operation on the SSC it inherits the last sort as applied in the default view of the filter in the standard VF page for the object that it being applied too! 

 

I don't know where on the object this information is stored at the moment or if it is even query-able.

 

public with sharing class ssctestcontroller {
	public integer pagesize {get;set;}
	public integer currentYear {get { return 2012; } set;}
	public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [SELECT Id, Name, StartDate, EndDate, Type, Status, Description, Owner.Name, NumberOfLeads, NumberOfContacts, NumberOfOpportunities, NumberOfWonOpportunities, AmountAllOpportunities, Exclude_from_Calendar__c FROM Campaign ORDER BY StartDate ASC LIMIT 2000]));
            }
            if(this.pagesize == null){
                this.pagesize = 2000;
            }
            this.setCon.setPageSize(this.pagesize);
            
            return setCon;
        }
        set;
    }
	
	public List<Campaign> getCampaigns(){
		setCon.setFilterId('_MY_FILTER_ID'); // when applied changes 'ORDER BY' regardless of DB QUERY in DB Locator.
		List<Campaign> returnedList = new List<Campaign>();
		List<Campaign> campaigns = setCon.getRecords();
		for(Campaign campaign : campaigns){
		 	if(campaign.startDate.Year()==this.currentYear || campaign.endDate.Year()==this.currentYear || 
	        	(campaign.startDate.Year()<=this.currentYear && campaign.endDate.Year()>=this.currentYear) )
	            	{
	                	returnedList.add(campaign);
	            	}
			}
		return returnedList;
	}	    
}

 

I'm going to try the other way of building the list first THAN APPLYING THE SSC to see if this affects the outcome.

 

UPDATE

Using the ALTERNATIVE method of applying the SSC doesn't affect 'ORDER BY' clauses when applying the filterId, see below:

 

	public List<Campaign> getCampaigns(){
		
		List<Campaign> setOfCampaigns = [SELECT Id, Name, StartDate, EndDate, Type, Status, Description, Owner.Name, NumberOfLeads, NumberOfContacts, NumberOfOpportunities, NumberOfWonOpportunities, AmountAllOpportunities, Exclude_from_Calendar__c FROM Campaign ORDER BY StartDate ASC LIMIT 2000];
		ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(setOfCampaigns);
		ssc.setFilterId('00BU0000002cdTIMAY');
		ssc.setPageSize(2000);
		/*
		List<Campaign> returnedList = new List<Campaign>();
		List<Campaign> campaigns = setCon.getRecords();
		for(Campaign campaign : campaigns){
		 	if(campaign.startDate.Year()==this.currentYear || campaign.endDate.Year()==this.currentYear || 
	        	(campaign.startDate.Year()<=this.currentYear && campaign.endDate.Year()>=this.currentYear) )
	            	{
	                	returnedList.add(campaign);
	            	}
			}
		return returnedList;*/
		return (ssc.getRecords());
	}	    

 

 


PreeSFDCDevPreeSFDCDev
Hi Jordan,

Can you please post the complete controller and VF you have used. I a facing the same issue.
.