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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Implement Pagination in wrapper class--Urgent Please help!!!

Hi All,
         I have this piece of code given below , Its a search page that displays results depending on search parametres and shows resuls with a checkbox next to them (Using Wrapper class). I wanna implement pagination here , Can anyone plz help me with the code?
Wrapper class line are highlighted.

public class AccountSearchController{

public List<Contact> ContactObjects {get;set;}
public List<Account> AccountObjects {get;set;}
public List<Account> AccountObjectsx {get;set;}
public Account AccObj {get;set;}
public Contact ContactObj {get;set;}
private String soql {get;set;}
//Our collection of the class/wrapper objects cContact 
public List<cContact> contactList {get; set;}
public ApexPages.StandardSetController con{get; set;}


////////////////////////////////////////////////////Constructor//////////////////////
public AccountSearchController(ApexPages.StandardController controller) {
  Value='Contact';
  ContactObj = new Contact();
  AccObj = new Account();
  // makes the defalut in the VF page for contact type as 'Main'
  ContactObj.Contact_Type__c ='Main';
  //pass value for picklist conatct type in query
  String slist = 'Main';
 
  // strings to pass defalut value of account segment and contact status and recordtype of account and contact as sales in query
  String defaultAccSegemnt= 'Regional';
  String defaultConStatus= 'Active';

  //list strings to pass account type in query as ('Active','Inactive','New')
  List <String> defaultAccType=new List<String>();
  String active= '\'Active\'';

  //userid = Userinfo.getUserId();
  userid='00550000000rgCd';
  system.debug('logged in user id>>>>>>>>>>'+ userid);
  
 
  soql ='SELECT Id,FirstName,LastName,Position__c,Contact.Account.IsPersonAccount,Contact.Account.Other_Address_Street_1__pc,Contact.Account.Phone ,Contact_Master_Policy_Mailing__c,Contact.Account.Type,Contact.Account.PY_Total_Ins_Volume__c,Contact_Type__c ,Phone,Email,Title, Contact.Account.Segment__c,Contact.Account.Parent_Name__c,Contact.Account.Branch_Org__c,Contact.Account.Name FROM Contact  ';

  runQuery();
 

}
public  PageReference runSearch() {
 
 

 
   Boolean masterPolicyBoolean=AccObj.Master_Policy__c;
  String masterPolicy;
  RecordType recordTypeValue= AccObj.RecordType;
  String recordType=String.valueof(recordTypeValue);

  if(Value == 'Contact'){
      soql ='SELECT Id,FirstName,LastName,Contact.Account.IsPersonAccount,Position__c,Contact.Account.Other_Address_Street_1__pc,Contact.Account.Phone,Contact_Master_Policy_Mailing__c,Contact.Account.Type,Contact.Account.PY_Total_Ins_Volume__c,Contact_Type__c ,Phone,Email,Title, Contact.Account.Parent_Name__c,Contact.Account.Segment__c,Contact.Account.Branch_Org__c,Contact.Account.Name FROM Contact WHERE  Contact.Account.Name!=null';
      soql += ' and Contact.Account.RecordTypeId =\''+String.escapeSingleQuotes(recordtypeAccount_sales)+'\'';
      soql += ' and Contact.RecordTypeId = \''+String.escapeSingleQuotes(recordtypeContact_sales)+'\'';
 
            if(AccountOwnerValue!=null && !AccountOwnerValue.equals('') && AccountOwnerValue.equals('My'))
                {soql += ' and Account.ownerId = \''+String.escapeSingleQuotes(userid)+'\'';
                system.debug('AccountOwnerValue soql -----------------------------'+AccountOwnerValue);
                system.debug('soql AccountOwnerValue -----------------------------'+soql );}


                }  
        }

                
   
  runQuery(); 
  return null;

}

public void runQuery() {


      soql+=  'Order By Contact.Account.Name';
          ContactObjects =Database.query(soql + ' limit 30');
          getContacts();
     

}

////////////////////////////Code Of Wrapper Class Starts here///////////

public List<cContact> getContacts() {
        system.debug('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
        //if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c:ContactObjects ) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                contactList.add(new cContact(c));
            }
            
        // }
        return contactList;
    }
// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
        //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
        public cContact(Contact c) {
        con = c;
        selected = false;
      }
}

public PageReference processSelected() {
    //We create a new list of Contacts that we be populated only with Contacts if they are selected
      List<Contact> selectedContacts = new List<Contact>();
      Set<Account> selectedAccounts = new Set<Account>();
      List<Account> selectedAccountList = new List<Account>();
      //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
      //for(cContact cCon: gatContacts()) {
      for(cContact cCon: contactList) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
                selectedAccounts.add(cCon.con.Account);
            }
        }
      // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
      for(Account acs: selectedAccounts){
          selectedAccountList.add(acs);
       }
       update selectedAccountList;
       update selectedContacts;
       contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
       return null;
   }




     

}
Best Answer chosen by Sonam (Salesforce Developers) 
Sonam_SFDCSonam_SFDC
Hi Shrey,

There is code available online to implement pagaination which I have also used in my test project when I implemented wrapper class:
Worth a read: http://salesforcehelpinghands.blogspot.sg/2012/10/pagination-with-maintaing-state-of.html



All Answers

shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com
The vf page dispalys results by iterating over contactList.
Sonam_SFDCSonam_SFDC
Hi Shrey,

There is code available online to implement pagaination which I have also used in my test project when I implemented wrapper class:
Worth a read: http://salesforcehelpinghands.blogspot.sg/2012/10/pagination-with-maintaing-state-of.html



This was selected as the best answer
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com
Hi ,
  Thanks a lot for your reply.
niharnihar
HI Sonam_SFDC,
Thabks for the code can you tell me how to implement the same code on LEAD and CONTACTS objects also.