Wrapper class and Pagination in Salesforce

    sfdcsharepoint.com

    This is most commonly used concept in apex.

    n

    What is the need for wrapper class?

    n

    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.

    n

    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.

    n

    For example we have list of opportunities along with checkbox,

    n

    Demo Site available here.

    wrapperclass

    Apex Class

    /**  n  *@Description:Extension Class for Querying the Data and setting the Pagination  n **/  n public with sharing class RecordSelectionPaginationController{  n   // Map stores all the records queried as per the pagenumber   n   Map> mapOpportunityPages = new Map>();   n   // Pagenumber  n   public integer intPageNumber{get;set;}  n   // total number of pages  n   public integer intNumberofPages{get;set;}  n   // returns the data to be displayed on the page  n   public List getLstOppRecords(){  n     List lstOpp= new List();  n     lstOpp.addAll(mapOpportunityPages.get(intPageNumber));  n     return lstOpp;  n   }  n   // Controller  n   public RecordSelectionPaginationController(ApexPages.StandardController controller) {  n   }  n   // sets the pagination data  n   public Pagereference setPagination() {  n     integer NumberofRecords = 0;  n     integer pagenumber = 1;  n     // Looping through the records of Opportunity  n     for (Opportunity objOpp :[SELECT Id,Name,StageName,Account.Name FROM Opportunity LIMIT 10000]) {  n       if (NumberofRecords < 10) {  n         if (mapOpportunityPages.containsKey(pagenumber)){  n           mapOpportunityPages.get(pagenumber).add(new cOpportunity(objOpp,false));  n         }  n         else {  n           mapOpportunityPages.put(pagenumber,new List{new cOpportunity(objOpp,false)});  n         }  n         NumberofRecords++;  n         if (NumberofRecords == 10) {  n           NumberofRecords = 0;  n           pagenumber++;  n         }  n       }  n     }  n     intNumberofPages = mapOpportunityPages.size();  n     intPageNumber =1;  n     getLstOppRecords();  n     return null;  n   }  n   public Boolean hasNext {  n     get {  n       if (intNumberofPages <= intPageNumber) {  n         return false;  n       }  n       else {  n         return true;  n       }  n     }  n     set;  n   }  n   public Boolean hasPrevious {  n     get {  n       if (intPageNumber == 1) {  n         return false;  n       }  n       else {  n         return true;  n       }  n     }  n     set;  n   }  n   public void first() {  n      intPageNumber = 1;  n      getLstOppRecords();  n   }  n   public void last() {  n      intPageNumber=intNumberofPages;   n      getLstOppRecords();  n   }   n   public void previous() {  n      intPageNumber--;  n      getLstOppRecords();  n   }  n    public void next() {  n      intPageNumber++;  n      getLstOppRecords();  n    }  n   // Wrapper to wrap the Checkbox value and Objrecord together  n   public class cOpportunity {  n     public Opportunity objOpp{get;set;}  n     public Boolean Selected{get;set;}   n     public cOpportunity(Opportunity objOpp,Boolean bSel) {  n       this.objOpp = objOpp;  n       this.Selected = bSel;  n     }   n   }  n }

    Visualforce Page:

      n     n      n     n       n         n           n         n         n         n         n       n       n       
    n n n n n n n n
    n
    n
    n
    n

    n

    #apex #Visualforce

    Leave a Reply

    Your email address will not be published. Required fields are marked *