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

StardardSetController QueryLocator Paging

Hi there,


I have a list of records(i.e Campaigns) and when the user select a campaign, I am displaying the related CampaignMembers in the bottom of the page. Now I wanted to implement paging, and after implementing the paging, it is not displaying anything.


This is the code that I have on the Controller:

public ApexPages.StandardSetController campaignMembers { get { if(campaignMembers == null) { campaignMembers = new ApexPages.StandardSetController( Database.getQueryLocator([SELECT Id, Lead.Country, Lead.City, Lead.FirstName, Lead.LastName, LeadId, Contact.MailingCountry, Contact.MailingCity, Contact.FirstName, Contact.LastName, ContactId FROM CampaignMember WHERE CampaignId = :campaign.Id])); // sets the number of records in each page set campaignMembers.setPageSize(5); } return campaignMembers; } set; } public List<CampaignMember> getMembers() { if(campaign == null) return new List<CampaignMember>(); return (List<CampaignMember>) campaignMembers.getRecords(); }


When the user selects a campaign I am setting the variable campaign on the controller. How do I re-initialize the QueryLocator with the new campaign.Id?


This seems like an issue for CampaignMember.


The following controller code works fine for contact, but it doesn't work for CampaignMember.


public with sharing class pagingController { // instantiate the StandardSetController from a query locator public ApexPages.StandardSetController con { get { if(con == null) { con = new ApexPages.StandardSetController( Database.getQueryLocator([ Select Id FROM CampaignMember Order By Id])); // sets the number of records in each page set con.setPageSize(5); } return con; } set; } // indicates whether there are more records after the current page set. public Boolean hasNext { get { return con.getHasNext(); } set; } // indicates whether there are more records before the current page set. public Boolean hasPrevious { get { return con.getHasPrevious(); } set; } // returns the page number of the current page set public Integer pageNumber { get { return con.getPageNumber(); } set; } // returns the first page of records public void first() { con.first(); } // returns the last page of records public void last() { con.last(); } // returns the previous page of records public void previous() { con.previous(); } // returns the next page of records public void next() {; } // returns the PageReference of the original page, if known, or the home page. public void cancel() { con.cancel(); } public List<CampaignMember> getContacts() { return (List<CampaignMember>) con.getRecords(); } }



Unfortunately campaign member can't be used with StandardSetController as there aren't list views for campaign members.


You might be able to fake it with a list that populates a data table, but I haven't tried this.


Hi John,


Can you expand on what you said about faking it with list to pulate the dataTable.  If you guide me with a sample code that would be great. Thanks.


Search for dataTable here to get sample code:


You'd return a list of campaign members instead of Accounts for your purpose.