You need to sign in to do that
Don't have an account?
Sameer Prasonn
SOQL Results pagination
I need to prepare result pagination. I must keep records limit to 100 for each page and once i'm done and click next It must fetch next 100 records and display next 100 records. I'm on the same. If any one can help that would be great Thank you in advance.
What I ended up doing was the following:
1. introduced a new field of type autonumber in the object, this helps to maintain the serial results.
2. fix the limit to 50 as following.
3. get the min for each iteration.
4. fire an SOQL Query for each iteration with the limit which i have declared above PAGE_SIZE
5. change the min each time and setup the max for the next iteration. that is on the basis of new field we have introduce in step 1.
6. total pages can be measured by get total no of results, that we can achieve via count.
The reson for this approach is it will never exceed the limit of offset ever.
I'll post the complete code for this scenario in my later post. hope this helps other as well.
Happy programming. keep on rocking guys.
All Answers
Please find the following the following page and the Class code which I have done way back.
Page Code:
*************
<apex:page controller="Sample" >
<style>
.ButtonCls{
colour:blue;
}
</style>
<apex:form >
<apex:pageBlock id="details" title="Members Available" helpUrl="https://help.salesforce.com" helpTitle="Help for this Page">
<table width="100%">
<apex:outputLabel style="color:red;font-size:15px">Total Records : {!count}</apex:outputLabel><br></br>
</table>
<apex:pageblockTable value="{!memb}" var="m">
<apex:column value="{!m.Name}"/>
<apex:column value="{!m.Age__c}"/>
</apex:pageblockTable>
<apex:pageblockButtons >
<apex:commandButton value="<<Start" rerender="details" action="{!beginning}" disabled="{!prev}" styleclass="ButtonCls" />
<apex:commandButton value="<Previous" rerender="details" action="{!previous}" disabled="{!prev}" styleclass="ButtonCls"/>
<apex:commandButton value="Next>" rerender="details" action="{!next}" disabled="{!nxt}" styleclass="ButtonCls"/>
<apex:commandButton value="End>>" rerender="details" action="{!end}" disabled="{!nxt}" styleclass="ButtonCls"/>
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
*********************
public class Sample
{
public integer totalRecs = 0;
public integer index = 0;
public integer blockSize = 5;
public integer count {get;set;}
public sample()
{
totalRecs = [select count() from Member__c];
count = totalRecs;
}
public List<Member__c> getMemb()
{
List<Member__c> membs = [SELECT Name, Age__c FROM Member__c LIMIT :blockSize OFFSET :index];
System.debug('Values are ' + membs);
return membs;
}
public void beginning()
{
index = 0;
}
public void previous()
{
index = index - blockSize;
}
public void next()
{
index = index + blockSize;
}
public void end()
{
index = totalrecs - math.mod(totalRecs,blockSize);
}
public boolean getprev()
{
if(index == 0)
return true;
else
return false;
}
public boolean getnxt()
{
if((index + blockSize) > totalRecs)
return true;
else
return false;
}
}
Regards,
Vijay Kumar
Ex : SELECT fieldLis FROM objectType [WHERE conditionExpression] ORDER BY fieldOrderByList LIMIT number_of_rows_to_return OFFSET number_of_rows_to_skip
But as Offset have Limit 2000 so you can only skip 2000 records using Offset if you're working on large set of data then I'll suggest you to filter your record based on Current records.
Please mark my answer as a best solution to your question to help others if it solves your problem
You can use setPageSize(100) to get 100 records at a time. Check out the StandardSetController methods to see how to implement the next/prev etc.. methods.
What I ended up doing was the following:
1. introduced a new field of type autonumber in the object, this helps to maintain the serial results.
2. fix the limit to 50 as following.
3. get the min for each iteration.
4. fire an SOQL Query for each iteration with the limit which i have declared above PAGE_SIZE
5. change the min each time and setup the max for the next iteration. that is on the basis of new field we have introduce in step 1.
6. total pages can be measured by get total no of results, that we can achieve via count.
The reson for this approach is it will never exceed the limit of offset ever.
I'll post the complete code for this scenario in my later post. hope this helps other as well.
Happy programming. keep on rocking guys.
1.) Using StandardSet Controller
2.) Soql with LIMIT and OFFSET