Wrapper class and Pagination in Salesforce
This is most commonly used concept in apex.
What is the need for wrapper class?
It is Inner class which consist of various different properties. For example, if you want to have single container which holds various types of variables or Sobject or Collection.
Second Example, Let’s say we want to showcase different opportunity along with Radio button or Checkbox and then by selecting those specific records you want to execute some logic on to this records.
For example we have list of opportunities along with checkbox,
Demo Site available here.

Apex Class
/**
*@Description:Extension Class for Querying the Data and setting the Pagination
**/
public with sharing class RecordSelectionPaginationController{
// Map stores all the records queried as per the pagenumber
Map<integer,List<cOpportunity>> mapOpportunityPages = new Map<integer,List<cOpportunity>>();
// Pagenumber
public integer intPageNumber{get;set;}
// total number of pages
public integer intNumberofPages{get;set;}
// returns the data to be displayed on the page
public List<cOpportunity> getLstOppRecords(){
List<cOpportunity> lstOpp= new List<cOpportunity>();
lstOpp.addAll(mapOpportunityPages.get(intPageNumber));
return lstOpp;
}
// Controller
public RecordSelectionPaginationController(ApexPages.StandardController controller) {
}
// sets the pagination data
public Pagereference setPagination() {
integer NumberofRecords = 0;
integer pagenumber = 1;
// Looping through the records of Opportunity
for (Opportunity objOpp :[SELECT Id,Name,StageName,Account.Name FROM Opportunity LIMIT 10000]) {
if (NumberofRecords < 10) {
if (mapOpportunityPages.containsKey(pagenumber)){
mapOpportunityPages.get(pagenumber).add(new cOpportunity(objOpp,false));
}
else {
mapOpportunityPages.put(pagenumber,new List<cOpportunity>{new cOpportunity(objOpp,false)});
}
NumberofRecords++;
if (NumberofRecords == 10) {
NumberofRecords = 0;
pagenumber++;
}
}
}
intNumberofPages = mapOpportunityPages.size();
intPageNumber =1;
getLstOppRecords();
return null;
}
public Boolean hasNext {
get {
if (intNumberofPages <= intPageNumber) {
return false;
}
else {
return true;
}
}
set;
}
public Boolean hasPrevious {
get {
if (intPageNumber == 1) {
return false;
}
else {
return true;
}
}
set;
}
public void first() {
intPageNumber = 1;
getLstOppRecords();
}
public void last() {
intPageNumber=intNumberofPages;
getLstOppRecords();
}
public void previous() {
intPageNumber--;
getLstOppRecords();
}
public void next() {
intPageNumber++;
getLstOppRecords();
}
// Wrapper to wrap the Checkbox value and Objrecord together
public class cOpportunity {
public Opportunity objOpp{get;set;}
public Boolean Selected{get;set;}
public cOpportunity(Opportunity objOpp,Boolean bSel) {
this.objOpp = objOpp;
this.Selected = bSel;
}
}
}
Visualforce Page:
<apex:page StandardController="Opportunity" action="{!setPagination}" extensions="RecordSelectionPaginationController">
<apex:sectionHeader title="Select" subtitle="Opportunities"/>
<apex:form >
<apex:pageBlock id="pageblock" title="Opportunity Selection" mode="maindetail">
<apex:pageBlockTable value="{!lstOppRecords}" var="Opp">
<apex:column width="20px" >
<apex:inputCheckbox value="{!Opp.Selected}"/>
</apex:column>
<apex:column value="{!Opp.objOpp.Name}"/>
<apex:column value="{!Opp.objOpp.StageName}"/>
<apex:column value="{!Opp.objOpp.Account.Name}"/>
</apex:pageBlockTable>
<apex:outputpanel >
<center>
<apex:panelGrid columns="5">
<apex:commandButton style="background:none!important;border:none;padding:0!important;cursor:pointer;" value="◄◄" reRender="pageblock" action="{!first}"/>
<apex:commandButton style="background:none!important;border:none;padding:0!important;cursor:pointer;" value="◄ Previous" reRender="pageblock" action="{!previous}" disabled="{!!hasPrevious}"/>
<apex:outputLabel value="{!intPageNumber} of {!intNumberofPages}" />
<apex:commandButton style="background:none!important;border:none;padding:0!important;cursor:pointer;" value="Next ►" reRender="pageblock" action="{!next}" disabled="{!!hasNext}"/>
<apex:commandButton style="background:none!important;border:none;padding:0!important;cursor:pointer;" value="►►" reRender="pageblock" action="{!last}"/>
</apex:panelGrid>
</center>
</apex:outputpanel>
</apex:pageBlock>
</apex:form>
</apex:page>