Pagination in Salesforce with Maintaining Selected Records

Share on facebook
Share on twitter
Share on linkedin
Pagination can be achieved in Salesforce, by using Standard Set Controller in salesforce; however, we cannot maintain the stage of the selected records in the pagination.

Pagination in salesforce can be achieved , by using Standard Set Controller in salesforce; however, we cannot maintain the stage of the selected records in the pagination. The following code explains how we can maintain the state of the selected records in pagination, and also how to process those selected records.

Take a demo at this page.

Public class WrapperContactWrapper

{

Public Contact con{get;set;}

Public boolean bool{get;set;}

public WrapperContactWrapper(Contact c,boolean bool)

{

this.con = c;

this.bool = bool;

}

}

Public class ContactTable

{

public String size { get; set; }

//This is Our collection of the class/wrapper objects WrapperContactWrapper

Public List<WrapperContactWrapper> wrapperlist;

Public Integer noOfRecords{get; set;}

// Create a new Map to verify whether the contact is already added in the Map

Map <id,Contact> SelectedcontactMap = new Map <id,Contact>();

public boolean display{get;set;}

public list<Contact> selectedList {get;set;}

// instantiate the StandardSetController from a query locator

public ApexPages.StandardSetController Setcon

{

get

{

if(Setcon == Null)

{

Setcon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name,Accountid,Email,MobilePhone,LeadSource from Contact]));

// sets the number of records in each page set

setCon.setpagesize(10);

noOfRecords = setCon.getResultSize();

}

return Setcon;

}

set;

}

//Returns a list of wrapper objects for the sObjects in the current page set

Public List<WrapperContactWrapper> getContact()

{

getSelectedContact();

// Initilaize the list to add the selected contact

wrapperlist = new List <WrapperContactWrapper>();

for(Contact cc : (List<contact>)Setcon.getRecords())

{

if( SelectedcontactMap .ContainsKey(cc.id))

{

wrapperlist.add (new WrapperContactWrapper(cc,true));

}

else

{

wrapperlist.add(new WrapperContactWrapper(cc,false));

}

}

return wrapperlist;}

public void getSelectedContact(){

if(wrapperlist!=null)

{

for(WrapperContactWrapper wr:wrapperlist)

{

if(wr.bool == true)

{

SelectedcontactMap.put(wr.con.id,wr.con); // Add the selected contact id in to the SelectedcontactMap.

}

else

{

SelectedcontactMap.remove(wr.con.id); // If you uncheck the contact, remove it from the selectedcontactMap

}

}

}

}

public void clickMe()

{

display = true;

getSelectedContact();


selectedList = SelectedcontactMap.values();

}

public integer pageNumber

{

get

{

return Setcon.getPageNumber();

}

set;

}

}

VF Page

<apex:page controller="ContactTable">

<apex:form id="myform">

<apex:pageblock >

<apex:pageBlockButtons location="Top">

<apex:commandButton value="Processed" action="{!clickMe}" />

</apex:pageBlockButtons>

<!-- In our table, we are displaying the Contact records -->

<apex:pageblocktable value="{!Contact}" var="cc" >

<apex:column headerValue="Choose Contact">

<!-- This is our selected Boolean property in our wrapper class -->

<apex:inputCheckbox value="{!cc.bool}"/>

</apex:column>

<!-- This is how we access the contact values within our Contact container/wrapper -->

<apex:column value="{!cc.con.Name}"/>

<apex:column value="{!cc.con.Accountid}"/>

<apex:column value="{!cc.con.Email}"/>

<apex:column Value="{!cc.con.MobilePhone}"/>

<apex:column value="{!cc.con.LeadSource}"/>

</apex:pageblocktable>

<apex:pageBlockButtons location="Bottom">

<apex:commandButton value="First" action="{!Setcon.First}" reRender="myform" />

<apex:commandButton value="Previous" action="{!Setcon.Previous}" reRender="myform" />

<apex:commandButton value="Next" action="{!Setcon.Next}" reRender="myform" />

<apex:commandButton value="Last" action="{!Setcon.Last}" reRender="myform" />

</apex:pageBlockButtons>

<apex:outputText >Page Number {!pageNumber} </apex:outputText>

</apex:pageblock>

<apex:pageblock >

<apex:outputPanel rendered="{!display}">

<apex:pageblocktable value="{!selectedList}" var="w" >

<apex:column value="{!w.Name}"/>

<apex:column value="{!w.Accountid}"/>

<apex:column value="{!w.Email}"/>

<apex:column value="{!w.MobilePhone}"/>

<apex:column value="{!w.LeadSource}"/>

</apex:pageblocktable>

</apex:outputPanel>

</apex:pageblock>

</apex:form>

</apex:page>

OutPut:

pagination in salesforce
what is pagination in salesforce
pagination in salesforce

Reference Link:

https://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardsetcontroller.htm

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Mobile Responsive Testing

Mobile Responsive Testing is simply a Testing Process performed to make sure that your website or an app is working appropriately on all types of devices.
Whether it an app or website, they need to ensure that they are accessible from any device, anytime, anywhere.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.