Using offset in soql

We can Use OFFSET keyword in SOQL to specify the starting row from the result  returned by your query. This is useful in cases where we need to quickly jump to a particular subset of the total result returned by SOQL query. For example if there are 20 records then, if we specify offset as 10 in the query then it would return record 11 through 20. In the following example org has total 10 account records (This are displayed in first table) and in the second table query is with offset 7, thus it return accounts 8 through 10 i.e only 3 records.

Visualforce Page
<apex:page controller="OffsetController">
  <apex:form >
    <apex:pageblock >
    Random 5 account records
      <apex:pageblocktable value="{!accAllList}" var="accAll">
        <apex:column value="{!}"/>
        <apex:column value="{!accAll.accountnumber}"/>
    <apex:pageblock >
    With Offset as  3,account records
      <apex:pageblocktable value="{!accOffsetList}" var="accOffset">
        <apex:column value="{!}"/>
        <apex:column value="{!accOffset.accountnumber}"/>

public class OffsetController {
    Public List<account> accAllList{get;set;}
    Public List<account> accOffsetList{get;set;}
    Public OffsetController(){
        accAllList = new List<account>();
        accOffsetList = new List<account>();
        accAllList = [select name,accountnumber from account ];
        accOffsetList = [select name,accountnumber from account OFFSET 7 ];

OFFSET keyword can be used to implement pagination in visaulforce page tables.

No comments:

Post a comment