Thursday 26 January 2017

Simple Pagination In salesforce

Hi Folks,

Its been long since I have written any blog. Please welcome me :P.

Ok. lets roll to business. I ll be sharing simple way of Pagination in salesforce. Lemme first Explain you the three variable I have used here in my Apex class :-

totalRecs = 0;
OffsetSize = 0;
LimitSize= 10;

totalRecs is used for tracking the total number of records that have fetched. So that I can deduct it fro the total number of records that have been shon so far in previous pages.

OffsetSize is used for showing the rest of the records on the current page number.

Limitsize is used for declaring the count of records that will be shown on every page.

So guys dnt worry here is the code:-




       public class Pagination
{
        private integer totalRecs = 0;
        private integer OffsetSize = 0;
        private integer LimitSize= 10;
        public Pagination()
        {
            totalRecs = [select count() from account];
        }
        public List<account> getacclist()
        {
            List<account> acc = Database.Query('SELECT Name, website, AnnualRevenue, description, Type FROM account LIMIT :LimitSize OFFSET :OffsetSize');
            System.debug('Values are ' + acc);
            return acc;
        }
        public void FirstPage()
        {
            OffsetSize = 0;
        }
        public void previous()
        {
            OffsetSize = OffsetSize - LimitSize;
        }public void next()
        {
            OffsetSize = OffsetSize + LimitSize;
        
        }
        public void LastPage()
        {
            OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
        }
        public boolean getprev()
        {
        if(OffsetSize == 0)
            return true;
            else
            return false;
        }
        public boolean getnxt()
        {
            if((OffsetSize + LimitSize) > totalRecs)
            return true;
            else
                return false;
        }
}




       <apex:page controller="Pagination" sidebar="false" showHeader="false">
<apex:form >
    <apex:pageBlock id="details">
        <apex:pageblockTable value="{!acclist}" var="acc">
            <apex:column value="{!acc.Name}"/>
            <apex:column value="{!acc.website}"/>
            <apex:column value="{!acc.AnnualRevenue}"/>
            <apex:column value="{!acc.Description}"/>
            <apex:column value="{!acc.Type}"/>
    </apex:pageblockTable>
<apex:pageblockButtons >
    <apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!prev}"/>
    <apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>
    <apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>
    <apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!nxt}"/>
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form></apex:page>



Thanks
Happy Coding !!

Cheers!!

oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...