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
newbiewvfdevnewbiewvfdev 

StandardSetController Question

Hi there,

 

I have implemented pagination for two objects contact and lead.  I have done this through StandardSetController.  The code is very similar for both except the QueryLocator is different.  I was thinking of merging both together and built something that would handle all of them. I am running into issues with this regard.

 

This is the original controller and visualforce page code which works with separate StandardSetController:

Controller: public with sharing class pagingControllerSeparate { Campaign campaign; public void pullCampaign() { con = null; lead = null; campaign = [SELECT Id, Name, NumberOfContacts, NumberOfLeads FROM Campaign WHERE Id = :'SomeID']; } public Campaign getCampaign() { if(campaign == null) campaign = new Campaign(); return campaign; } // instantiate the StandardSetController from a query locator public ApexPages.StandardSetController con { get { if(con == null) { con = new ApexPages.StandardSetController( Database.getQueryLocator([ SELECT Id, FirstName, LastName, MailingCity, MailingCountry FROM Contact Where Id in ( Select ContactId From CampaignMember where campaignId =:campaign.Id And ContactId <> null)])); // 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() { con.next(); } // returns the PageReference of the original page, if known, or the home page. public void cancel() { con.cancel(); } public List<Contact> getContacts() { return (List<Contact>) con.getRecords(); } // instantiate the StandardSetController from a query locator public ApexPages.StandardSetController lead { get { if(lead == null) { lead = new ApexPages.StandardSetController( Database.getQueryLocator([ SELECT Id, FirstName, LastName, City, Country FROM Lead Where Id in ( Select LeadId From CampaignMember where CampaignId =:campaign.Id And LeadId <> null)])); // sets the number of records in each page set lead.setPageSize(5); } return lead; } set; } // indicates whether there are more records after the current page set. public Boolean hasNextLead { get { return lead.getHasNext(); } set; } // indicates whether there are more records before the current page set. public Boolean hasPreviousLead { get { return lead.getHasPrevious(); } set; } // returns the page number of the current page set public Integer pageNumberLead { get { return lead.getPageNumber(); } set; } // returns the first page of records public void firstLead() { lead.first(); } // returns the last page of records public void lastLead() { lead.last(); } // returns the previous page of records public void previousLead() { lead.previous(); } // returns the next page of records public void nextLead() { lead.next(); } // returns the PageReference of the original page, if known, or the home page. public void cancelLead() { lead.cancel(); } public List<Lead> getLeads() { return (List<Lead>) lead.getRecords(); } } Visualforce Page: <apex:page Controller="pagingControllerSeparate"> <style> .activeTab {background-color: #236FBD; color:white; background-image:none} .inactiveTab { background-color: lightgrey; color:black; background-image:none} </style> <apex:form > <apex:commandLink value="Select" action="{!pullCampaign}" styleClass="btn" rerender="campaignContactsAndLeads"> <apex:param name="campaignId" value="{!campaign.Id}" /> </apex:commandLink> <apex:outputPanel id="campaignContactsAndLeads" > <apex:tabPanel switchType="client" selectedTab="tabCampaigns" id="MailingListTabPanel" rendered="{!IF(ISNULL(campaign.Id), false, true)}" tabClass="activeTab" inactiveTabClass="inactiveTab"> <apex:tab label="Contacts({!campaign.NumberOfContacts})" name="contactSec" id="tabContacts"> <apex:pageBlock title="Category Results - Page #{!pageNumber}" > <apex:pageBlockTable id="cMContacts" value="{!Contacts}" var="cont"> <apex:column headerValue="First Name"> <apex:outputField value="{!cont.FirstName}"/>&nbsp; </apex:column> <apex:column headerValue="Last Name"> <apex:outputField value="{!cont.LastName}" />&nbsp; </apex:column> <apex:column headerValue="Mailing City"> <apex:outputField value="{!cont.MailingCity}"/>&nbsp; </apex:column> <apex:column headerValue="Mailing Country"> <apex:outputField value="{!cont.MailingCountry}" />&nbsp; </apex:column> </apex:pageBlockTable> </apex:pageBlock> <apex:panelGrid columns="4" id="contactLinks"> <apex:commandLink action="{!first}" rerender="cMContacts, contactLinks">First</apex:commandlink> <apex:commandLink action="{!previous}" rerender="cMContacts, contactLinks" rendered="{!hasPrevious}">Previous</apex:commandlink> <apex:commandLink action="{!next}" rendered="{!hasNext}" rerender="cMContacts, contactLinks">Next</apex:commandlink> <apex:commandLink action="{!last}" rerender="cMContacts, contactLinks">Last</apex:commandlink> </apex:panelGrid> </apex:tab> <apex:tab label="Leads({!campaign.NumberOfLeads})" name="leadSec" id="tabLeads"> <apex:pageBlock title="Category Results - Page #{!pageNumberLead}" > <apex:pageBlockTable id="cMLeads" value="{!Leads}" var="leadM"> <apex:column headerValue="First Name"> <apex:outputField value="{!leadM.FirstName}"/>&nbsp; </apex:column> <apex:column headerValue="Last Name"> <apex:outputField value="{!leadM.LastName}" />&nbsp; </apex:column> <apex:column headerValue="Mailing City"> <apex:outputField value="{!leadM.City}"/>&nbsp; </apex:column> <apex:column headerValue="Mailing Country"> <apex:outputField value="{!leadM.Country}" />&nbsp; </apex:column> </apex:pageBlockTable> </apex:pageBlock> <apex:panelGrid columns="4" id="leadLinks"> <apex:commandLink action="{!firstLead}" rerender="cMLeads, leadLinks">First</apex:commandlink> <apex:commandLink action="{!previousLead}" rerender="cMLeads, leadLinks" rendered="{!hasPreviousLead}">Previous</apex:commandlink> <apex:commandLink action="{!nextLead}" rerender="cMLeads, leadLinks" rendered="{!hasNextLead}">Next</apex:commandlink> <apex:commandLink action="{!lastLead}" rerender="cMLeads, leadLinks">Last</apex:commandlink> </apex:panelGrid> </apex:tab> </apex:tabPanel> </apex:outputPanel> </apex:form> </apex:page>

 

I am trying to merge both paging code into one so I could use one StandardSetController to handle both? Is this possible and how can I approach this?

 

I tried creating a property and based on the property I set the QueryLocator.

 

Here is what I have so far. But the problem is, it renderes fine, but when I click Next, Previous or any of the buttons, it doesn't do anything. Please help. Thanks.

newbiewvfdevnewbiewvfdev

Here is what I have so far:

 

Controller: public with sharing class pagingController { Campaign campaign; String m_objectType; public void pullCampaign() { setController = null; campaign = [SELECT Id, Name, NumberOfContacts, NumberOfLeads FROM Campaign WHERE Id = :'701A00000000kGz']; } public Campaign getCampaign() { if(campaign == null) campaign = new Campaign(); return campaign; } public String ObjectType { get { return m_objectType; } set { m_objectType = value; } } // instantiate the StandardSetController from a query locator public ApexPages.StandardSetController setController { get { if(setController == null) { if(ObjectType == 'Lead') { setController = new ApexPages.StandardSetController( Database.getQueryLocator([ SELECT Id, FirstName, LastName, City, Country FROM Lead Where Id in ( Select LeadId From CampaignMember where CampaignId =: campaign.Id And LeadId <> null)])); } else if(ObjectType == 'Contact') { setController = new ApexPages.StandardSetController( Database.getQueryLocator([ SELECT Id, FirstName, LastName, MailingCity, MailingCountry FROM Contact Where Id in ( Select ContactId From CampaignMember where CampaignId =: campaign.Id And ContactId <> null)])); } // sets the number of records in each page set setController.setPageSize(5); } return setController; } set; } // indicates whether there are more records after the current page set. public Boolean hasNext { get { return setController.getHasNext(); } set; } // indicates whether there are more records before the current page set. public Boolean hasPrevious { get { return setController.getHasPrevious(); } set; } // returns the page number of the current page set public Integer pageNumber { get { return setController.getPageNumber(); } set; } // returns the first page of records public void first() { setController.first(); } // returns the last page of records public void last() { setController.last(); } // returns the previous page of records public void previous() { setController.previous(); } // returns the next page of records public void next() { setController = null; setController.next(); } public List<Lead> getLeadsFromSetController() { setController = null; ObjectType = 'Lead'; return (List<Lead>) setController.getRecords(); } public List<Contact> getContactsFromSetController() { setController = null; ObjectType = 'Contact'; return (List<Contact>) setController.getRecords(); } } Visualforce page: <apex:page Controller="pagingController"> <style> .activeTab {background-color: #236FBD; color:white; background-image:none} .inactiveTab { background-color: lightgrey; color:black; background-image:none} </style> <apex:form > <apex:commandLink value="Select" action="{!pullCampaign}" styleClass="btn" rerender="campaignContactsAndLeads"> <apex:param name="campaignId" value="{!campaign.Id}" /> </apex:commandLink> <apex:outputPanel id="campaignContactsAndLeads" > <apex:tabPanel switchType="client" selectedTab="tabCampaigns" id="MailingListTabPanel" rendered="{!IF(ISNULL(campaign.Id), false, true)}" tabClass="activeTab" inactiveTabClass="inactiveTab"> <apex:tab label="Contacts({!campaign.NumberOfContacts})" name="contactSec" id="tabContacts"> <apex:pageBlock title="Category Results - Page #{!pageNumber}" > <apex:pageBlockTable id="cMContacts" value="{!ContactsFromSetController}" var="cont"> <apex:column headerValue="First Name"> <apex:outputField value="{!cont.FirstName}"/>&nbsp; </apex:column> <apex:column headerValue="Last Name"> <apex:outputField value="{!cont.LastName}" />&nbsp; </apex:column> <apex:column headerValue="Mailing City"> <apex:outputField value="{!cont.MailingCity}"/>&nbsp; </apex:column> <apex:column headerValue="Mailing Country"> <apex:outputField value="{!cont.MailingCountry}" />&nbsp; </apex:column> </apex:pageBlockTable> </apex:pageBlock> <apex:panelGrid columns="4" id="contactLinks"> <apex:commandLink action="{!first}" rerender="cMContacts, contactLinks">First</apex:commandlink> <apex:commandLink action="{!previous}" rerender="cMContacts, contactLinks" rendered="{!hasPrevious}">Previous</apex:commandlink> <apex:commandLink action="{!next}" rendered="{!hasNext}" rerender="cMContacts, contactLinks">Next</apex:commandlink> <apex:commandLink action="{!last}" rerender="cMContacts, contactLinks">Last</apex:commandlink> </apex:panelGrid> </apex:tab> <apex:tab label="Leads({!campaign.NumberOfLeads})" name="leadSec" id="tabLeads"> <apex:pageBlock title="Category Results - Page #{!pageNumber}" > <apex:pageBlockTable id="cMLeads" value="{!LeadsFromSetController}" var="leadM"> <apex:column headerValue="First Name"> <apex:outputField value="{!leadM.FirstName}"/>&nbsp; </apex:column> <apex:column headerValue="Last Name"> <apex:outputField value="{!leadM.LastName}" />&nbsp; </apex:column> <apex:column headerValue="Mailing City"> <apex:outputField value="{!leadM.City}"/>&nbsp; </apex:column> <apex:column headerValue="Mailing Country"> <apex:outputField value="{!leadM.Country}" />&nbsp; </apex:column> </apex:pageBlockTable> </apex:pageBlock> <apex:panelGrid columns="4" id="leadLinks"> <apex:commandLink action="{!first}" rerender="cMLeads, leadLinks">First</apex:commandlink> <apex:commandLink action="{!previous}" rerender="cMLeads, leadLinks" rendered="{!hasPrevious}">Previous</apex:commandlink> <apex:commandLink action="{!next}" rerender="cMLeads, leadLinks" rendered="{!hasNext}">Next</apex:commandlink> <apex:commandLink action="{!last}" rerender="cMLeads, leadLinks">Last</apex:commandlink> </apex:panelGrid> </apex:tab> </apex:tabPanel> </apex:outputPanel> </apex:form> </apex:page>