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
Sudhir_MeruSudhir_Meru 

Pagination on VisualForce Page using multiple soql queries.

Hi, 

 Visulaforce and Controller which i have created is conditionally SOQL I tried few methods which posted in few blogs but i couldnt sucess.

 In controller I am limiting query to display only 10 records for now. But there are more than 1000k records. Please suggest me how to achive pagination on my requirement. 

Controller

public class Renwal_Account_test
{
  //URL Passing Parameters.
  String PageContractId = ApexPages.currentPage().getParameters().get('ContractId');    
  String PageAccountId  = ApexPages.currentPage().getParameters().get('AccountId');  
 
  //Our collection to class/wrapper objects wrapAsset
  public List<wrapAsset> wrapAssetList {get; set;}
  public List<Asset> selectedAssets{get;set;}    
   
  public Renwal_Account_test()
  { 
   GetAccount = new Asset();    //Get Account Id from Page
   GetContract = new Asset();   //Get Contract Name from Page  
   Filter_Account_Records();
   }
 
  public Asset GetAccount{get;set;}
  public Asset GetContract{get;set;}
   
  public List<Asset> Account_yourObjList{get;set;}
  public List<Asset> Asset_yourObjList{get;set;}  
  public List<Asset> Default_Asset_yourObjList{get;set;} 
  
  //Function to Return Asset Report Passing Account ID   
  public void Filter_Account_Records(){
    Account_yourObjList = new List<Asset>();
   
  If ( GetAccount.AccountId == NULL || GetContract.Name == NULL )
  {
    If ( PageContractId <> NULL )
     {
      Contract C;
      C = [SELECT Name FROM Contract WHERE Id = :PageContractId Limit 1];
      Account_yourObjList = [SELECT Id,AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate from Asset WHERE last_contract_number__c = :C.Name LIMIT 10];                      
     }      
    else If ( PageAccountId <> NULL )
     {
      Account_yourObjList = [SELECT Id,AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate from Asset WHERE AccountId = :PageAccountId LIMIT 10];                        
     } 
  } 
      
  If ( GetAccount.AccountId <> NULL && GetContract.Name == NULL )
    {  
    Account_yourObjList = [SELECT Id,AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate from Asset WHERE AccountId = :GetAccount.AccountId LIMIT 10];                      
    }
  else If ( GetContract.Name <> NULL && GetAccount.AccountId == NULL )
    {  
    Account_yourObjList = [SELECT Id,AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate from Asset WHERE last_contract_number__c  = :GetContract.Name LIMIT 10];                      
    }
  else If ( GetAccount.AccountId <> NULL && GetContract.Name <> NULL )
    {
    Account_yourObjList = [SELECT Id,AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate from Asset WHERE AccountId = :GetAccount.AccountId and last_contract_number__c = :GetContract.Name LIMIT 10];
    }      
  
   // Selection of Row to Temp Collection
   //if(wrapAssetList == null) {
     wrapAssetList = new List<wrapAsset>();
       for(Asset a: Account_yourObjList) {
        wrapAssetList.add(new wrapAsset(a));       
       }
          //  }   
                
  } 
 
  // Store Selected Records to Object/Table
public void processSelected() {
    selectedAssets = new List<Asset>();
    list<Temp_Assets__c> TempAssetList = new list<Temp_Assets__c>();
   
        for(wrapAsset wrapAssetObj : wrapAssetList) {
            if(wrapAssetObj.selected == true) {                              
                selectedAssets.add(wrapAssetObj.acc);                                 
            }           
        }
       
       for(Asset Act : selectedAssets)
            {
              Temp_Assets__c TempAsset = new Temp_Assets__c();
              TempAsset.Name = 'Sudhir';
              TempAsset.AccountId__c = Act.AccountId;
              TempAsset.Product__c = Act.Product2Id;
              TempAsset.Serial_Number__c = Act.SerialNumber;
              TempAsset.Last_Contract_Number__c = Act.last_contract_number__c;
              TempAsset.Service_Start_Date__c = Act.Service_Start_Date_Min__c;
              TempAsset.Service_End_Date__c = Act.Service_End_Date_Max__c;
              TempAsset.Install_Date__c = Act.InstallDate;                           
              TempAssetList.add(TempAsset);
            }
           
        Insert TempAssetList;
    } 
   
    // This is our wrapper/container class.
    public class wrapAsset {
        public Asset acc {get; set;}
        public Boolean selected {get; set;}

        public wrapAsset(Asset a) {
            acc = a;
            selected = false;
        }
    }

  }

VisualForce 

<apex:page title="Renewal Quote" controller="Renwal_Account_test" showHeader="false" sidebar="false" readOnly="false" cache="false">

<script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
</script>
   
<apex:sectionHeader subtitle="Create Renewal Quote" title="Meru Networks"/>

<apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Filters" columns="2">
<apex:inputField value="{!GetAccount.AccountId}"/>
<apex:commandButton action="{!Filter_Account_Records}" value="Fetch Data"></apex:commandbutton>
<apex:inputField value="{!GetContract.Name}" required="false"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

<apex:form >
        <apex:pageBlock id="details" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save Records" action="{!processSelected}" rerender="table2"/>
                                               
            </apex:pageBlockButtons>

            <apex:pageblockSection title="Install Base Report" collapsible="false" columns="1">

                <apex:pageBlockTable value="{!wrapAssetList}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.AccountId}" />
                    <apex:column value="{!accWrap.acc.Product2Id}" />
                    <apex:column value="{!accWrap.acc.SerialNumber}" />
                    <apex:column value="{!accWrap.acc.last_contract_number__c}" />
                    <apex:column value="{!accWrap.acc.Service_Start_Date_Min__c}" />
                    <apex:column value="{!accWrap.acc.Service_End_Date_Max__c}" />
                    <apex:column value="{!accWrap.acc.InstallDate}" />                   
                </apex:pageBlockTable>
     
                 <apex:pageBlockTable value="{!selectedAssets}" var="c" id="table2" title="Selected Assets">
                    <apex:column value="{!c.AccountId}" headerValue="Account Name"/>
                     <apex:column value="{!c.Product2Id}" headerValue="Product" />
                    <apex:column value="{!c.SerialNumber}" headerValue="Serial Number"/>  
                    <apex:column value="{!c.last_contract_number__c}" headerValue="Last Contract Number"/>
                    <apex:column value="{!c.Service_Start_Date_Min__c}" headerValue="Service Start Date" />
                    <apex:column value="{!c.Service_End_Date_Max__c}" headerValue="Service End Date"/>
                    <apex:column value="{!c.InstallDate}" headerValue="Install Date" />                
                </apex:pageBlockTable>
       
         
          
            </apex:pageblockSection>
        </apex:pageBlock>
       
    </apex:form>




</apex:page>

Thanks
Sudhir
ppoojary18@gmail.comppoojary18@gmail.com
Pagination can easily achived by using StandardSetController and OFFSET.
Example : https://developer.salesforce.com/page/Paginating_Data_for_Force.com_Applications

In your case, i strongly suggest you for pagination using offset as you are doing DML operation, find some Example of OFFSET here http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_offset.htm
Sudhir_MeruSudhir_Meru

Hi Poojary, 

  Thanks for your suggestion. Can you tweek my code. I am not getting how to modify Please suggest me with my code ill continue the same. 

Thanks

Sudhir

Sudhir_MeruSudhir_Meru
Any one Please suggest me a sample code on my class which I have created.