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
Samuel RobertSamuel Robert 

Pagination using Custom Controller

Hi,

I have devloped a pagination for custom  controllor, however i have finding the issue as, while i select the category as opened and hit on search it will list the set of records and i will hit on the next page botton to check the second set of records, now im on the second page. At this stage i again change the category to closed and hit on search it will list the set of records but still it is on second page not on the first page. Please Help me on this.

Please find the code below....

 
<apex:page controller="CaseAssocInTriageController" sidebar="False">
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />            
    <apex:stylesheet value="{!URLFOR($Resource.OnlineOrderFormCss, 'OnlineOrderForm.css')}" />
    <apex:includeScript value="{!URLFOR($Resource.OnlineOrderFormScript, 'OnlineOrderFormScript.js')}"/>
    <style type="text/css">    
        td {        
        valign: middle!important;
        padding: 10px;        
        }
        
        .btnblue{
        margin-bottom:0px!important;        
        } 
        
        body .btnDisabled:hover{
        color: #909090!important;
        }
        body .btnDisabled{
        color: #909090!important;
        }  
        
        td .redbg{
        background-color:red!important;
        width:50px!important;
        height:50px!important;
        border:0px!important;
        margin:0px!important;
        }
        
        td .greenbg{
        background-color:green!important;
        width:50px!important;
        height:50px!important;
        border:0px!important;
        margin:0px!important;
        }
        
        td .yellowbg{
        background-color:yellow!important;
        width:50px!important;
        height:50px!important;
        border:0px!important;
        margin:0px!important;
        }
    </style>
    <apex:pageBlock title="Triage SLA's Dashboard" id="casesref">
        <apex:pageBlockButtons >                                                                                  
            <apex:actionstatus id="ocstatus">
                <apex:facet name="start">
                    <div class="waitingSearchDiv" id="el_loading" style="background-color: #fbfbfb;
                                                                         height: 100%;opacity:0.65;width:100%;"> 
                        <div class="waitingHolder">
                            <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />
                            <span class="waitingDescription">Please Wait...</span>
                        </div>
                    </div>
                </apex:facet>
            </apex:actionstatus>
        </apex:pageBlockButtons> 
        <apex:form >
            <apex:pageBlockSection columns="1">
                <apex:outputPanel id="ref">
                    <table colspan="3">
                        <tr>
                            <td><apex:outputLabel value="Case Status " style="font-weight:bold"/></td>
                            <td>                    
                                <apex:selectList size="1" value="{!caseStatus}" style="width:100px;Height:25px;">
                                    <apex:selectOption itemValue="Open"></apex:selectOption>
                                    <apex:selectOption itemValue="Closed"></apex:selectOption>
                                </apex:selectList> 
                            </td>                               
                            <td>
                                <apex:outputLabel value=" Account Name " style="font-weight:bold;"/>                    
                            </td>
                            <td>
                                <apex:selectList size="1" value="{!AcctName}" style="Height:25px;">
                                    <apex:selectOptions value="{!AcctNames}"></apex:selectOptions>
                                </apex:selectList>                            
                            </td>
                            <td></td>
                            <td>
                                <apex:commandButton value="Search" action="{!search}" styleClass="btnblue" status="ocstatus" reRender="ref,rest,bottompg,toppg,casesref">                                    
                                </apex:commandButton>
                            </td> 
                            <td>
                                
                            </td>
                        </tr>
                    </table>                     
                </apex:outputPanel> 
                <apex:outputPanel style="float:right;" id="toppg">
                    <apex:outputLabel value="Please choose options to view data.. " rendered="{! not isRecordFound}" style="font-weight:bold;color:#25729a;width:300px;"></apex:outputLabel>
                    <apex:outputLabel value="   {!pageInfo} : " rendered="{!isRecordFound}" style="font-weight:bold;color:#25729a;width:300px;"></apex:outputLabel>  
                    <apex:commandButton value="<<Previous page" action="{!searchPreviousPageOffset}"   status="ocstatus" reRender="casesref,toppg,rest" disabled="{!disablePreviousBtn}" style="color:#25729a">                    
                    </apex:commandButton>                                                  
                    <apex:commandButton value="Next page>>" action="{!searchNextPageOffset}"  status="ocstatus" reRender="casesref,toppg,rest,ref" disabled="{!disableNextBtn}" style="color:#25729a">                    
                    </apex:commandButton>
                </apex:outputPanel>
                <apex:outputPanel id="rest">   
                    <apex:pageBlockTable value="{!caseResults}" var="cr">
                        <apex:column headerValue="Case Number">
                            <apex:outputLink value="{!URLFOR($Action.Case.View, cr.Id)}" target="_blank">
                                {!cr.casenumber}
                            </apex:outputLink>                            
                        </apex:column>
                        <apex:column headerValue="Triage Date" value="{!cr.Date_Swap_Triage_New__c}">
                            <apex:facet name="header"></apex:facet>                             
                        </apex:column>
                        <apex:column headerValue="Age in Hours" value="{!cr.CaseHourCalc__c}">
                            <apex:facet name="header"></apex:facet>                             
                        </apex:column>
                        <apex:column style="padding:0px!important">                            
                            <apex:facet name="header">Severity</apex:facet>
                            <apex:outputPanel styleClass="redbg" rendered="{!(cr.priority=='Normal' && cr.CaseHourCalc__c>=31 && cr.status!='Closed') || (cr.priority=='High' && cr.CaseHourCalc__c>=6 && cr.status!='Closed') || (cr.priority=='Urgent' && cr.CaseHourCalc__c>=4 && cr.status!='Closed')}">
                                <div class="redbg"></div>
                            </apex:outputPanel>
                            <apex:outputPanel styleClass="yellowbg" rendered="{!(cr.priority=='Normal' && cr.CaseHourCalc__c>=24  && cr.CaseHourCalc__c<32 && cr.status!='Closed') || (cr.priority=='High' && cr.CaseHourCalc__c>=4 && cr.CaseHourCalc__c<6 && cr.status!='Closed') || (cr.priority=='Urgent' && cr.CaseHourCalc__c>=2 && cr.CaseHourCalc__c<4 && cr.status!='Closed')}">
                                <div class="yellowbg"></div>
                            </apex:outputPanel>
                            <apex:outputPanel styleClass="greenbg" rendered="{!(((cr.priority=='Normal' && cr.CaseHourCalc__c<24) || (cr.priority=='High' && cr.CaseHourCalc__c<4) || (cr.priority=='Urgent' && cr.CaseHourCalc__c<2)) || cr.status=='Closed')}">
                                <div class="greenbg"></div>
                            </apex:outputPanel>                                 
                        </apex:column>
                        <apex:column headerValue="Case Priority" value="{!cr.priority}" >
                            <apex:facet name="header">
                                <apex:commandLink action="{!sortByCasePriority}" 
                                                  reRender="rest" status="ocstatus">Case Priority
                                </apex:commandLink>
                            </apex:facet>                            
                        </apex:column>
                        <apex:column headerValue="Account Name" value="{!cr.account.name}">
                            <apex:facet name="header">
                                <apex:commandLink action="{!sortByCaseAccount}" 
                                                  reRender="rest" status="ocstatus">Account Name
                                </apex:commandLink>
                            </apex:facet>                            
                        </apex:column>                      
                        <apex:column headerValue="Subject" style="width:50%">
                            <apex:outputText value="{!cr.Subject}"></apex:outputText>
                        </apex:column>
                        <apex:column headerValue="Case Status" value="{!cr.status}">
                            <apex:facet name="header">
                                <apex:commandLink action="{!sortByCaseStatus}" 
                                                  reRender="rest" status="ocstatus">Case Status
                                </apex:commandLink>
                            </apex:facet>                            
                        </apex:column>
                        <apex:column headerValue="Case Owner" value="{!cr.Owner.name}">
                            <apex:facet name="header">
                                <apex:commandLink action="{!sortByCaseOwner}" 
                                                  reRender="rest" status="ocstatus">Case Owner
                                </apex:commandLink>
                            </apex:facet>                            
                        </apex:column>                                                  
                        <apex:column headerValue="Rally ID" value="{!cr.Rally_ID__c}">
                            <apex:facet name="header">
                                <apex:commandLink action="{!sortByRallyID}" 
                                                  reRender="rest" status="ocstatus">Rally ID
                                </apex:commandLink>
                            </apex:facet>                               
                        </apex:column>
                        <apex:column headerValue="Rally Development Status" value="{!cr.RALLYSM__Development_Status__c}">
                            <apex:facet name="header" >
                                <apex:commandLink action="{!sortByRallyDS}" 
                                                  reRender="rest" status="ocstatus">Rally Development Status
                                </apex:commandLink>
                            </apex:facet>                            
                        </apex:column>                                                                                                                         
                    </apex:pageBlockTable>
                </apex:outputPanel> 
                <apex:outputPanel style="float:right;" id="bottompg">
                    <apex:commandButton value="<<Previous page" action="{!searchPreviousPageOffset}"   status="ocstatus" reRender="casesref,toppg" disabled="{!disablePreviousBtn}" style="color:#25729a">                    
                    </apex:commandButton>                                                  
                    <apex:commandButton value="Next page>>" action="{!searchNextPageOffset}"  status="ocstatus" reRender="casesref,toppg  " disabled="{!disableNextBtn}" style="color:#25729a">                    
                    </apex:commandButton>
                </apex:outputPanel>
            </apex:pageBlockSection>            
        </apex:form>        
    </apex:pageBlock>        
</apex:page>
 
public class CaseAssocInTriageController {
    public string rallyType {get;set;}
    public string AcctName {get;set;}
    public string caseStatus {get;set;}
    public string severity {get;set;}
    public Case caseAssoc {get; set;}
    
    public Integer nextPageOffset{get; set;}
    public Integer previousPageOffset{get; set;}
    public Integer currentPageOffset{get; set;}
    
    public boolean disableNextBtn{get{
        return nextPageOffset==0 ? true : ((Math.mod(nextPageOffset, 2) >0) ? true : false);
            }set;}
    
    
    public boolean disablePreviousBtn{get{     
        return  (currentPageOffset == 0 && previousPageOffset == 0) ? true : false;    
    } set;}    
    
    public void searchNextPageOffset()
    {
        currentPageOffset = nextPageOffset;
        if(Test.isRunningTest())
            currentPageOffset= 0;
        search();
    }
    
    public void searchPreviousPageOffset()
    {
        currentPageOffset = previousPageOffset;
        if(Test.isRunningTest())
            currentPageOffset= 0;
        search();
    }
    
    public boolean isRecordFound{get{
        return caseResults==null ? false : (caseResults.isEmpty() ? false : true);
            }set;}
    
    public Integer pagerecordCount{
        get
        {
            return caseResults==null ? 0 : (caseResults.isEmpty() ? 0 : caseResults.size());
                } 
        set;
    }
    
    public integer recordCount{get;set;}
    
    public String pageInfo{get{
        return 'Viewing (' + currentPageOffset + ' to ' + ((currentPageOffset + 2) >recordCount ? recordCount : (currentPageOffset + 2))  +') / ' + recordCount;
    }set;}
    
    public CaseAssocInTriageController(){
        if(AcctNames==null || AcctNames.isEmpty())        
            AcctNames = fetchAcctNames();     
        
        sortName = 'Owner.name';
        sortBy = 'ASC';
        ascdesvOwner=-1;
        ascdesvCAccount =-1;
        ascdesvCPriority=-1;
        ascdesvCStatus = -1;
        ascdesvRDS =-1;
        ascdesvRID =-1;
        
        nextPageOffset= 0;
        previousPageOffset = 0;
        currentPageOffset = 0;
        
    }
    
    public list<SelectOption> AcctNames {get;set;}    
    public list<SelectOption> fetchAcctNames(){
        list<SelectOption> AcctOptions = new list<SelectOption>();
        
        Integer reccount = Database.countQuery('select count() from account where type=\'customer\' ');
        
        decimal val = reccount/1000; 
        Integer loopCount = val.intValue() + (Math.mod(reccount,1000) > 0 ? 1 : 0);
        if(val==0)
            loopCount=1;
        
        system.debug('rec  : ' + reccount);
        system.debug('rec  : ' + val);
        system.debug('rec  : ' + loopCount);
        
        list<Account> acct = null;
        for(Integer index=0; index<loopCount; index++)
        {
            Integer offsetcount = index * 1000; 
            acct = [Select id,name from Account where type IN ('customer') limit 1000 OFFSET :offsetcount];
            if(acct != null || !acct.isEmpty()){
                AcctOptions.add(new SelectOption('All','All'));  
                for(Account acc: acct)
                    AcctOptions.add(new SelectOption(acc.Name,acc.Name));            
            }
        }
        return AcctOptions;
    } 
    
    public String sortName{get;set;}
    public String sortBy{get;set;}
    
    public Integer ascdesvOwner{get; set;}
    public void sortByCaseOwner()
    {
        sortName = 'Owner.name';
        ascdesvOwner = ascdesvOwner== -1  ? 0 : (ascdesvOwner== 1 ? 0 : 1); 
            sortBy = ascdesvOwner ==0 ? 'ASC' : 'DESC';  
        System.debug('ascdesvOwner : ' + ascdesvOwner);
        search();
    }
    
    public Integer ascdesvCStatus{get; set;}
    public void sortByCaseStatus()
    {
        sortName = 'Status';
        ascdesvCStatus = ascdesvCStatus== -1  ? 0 : (ascdesvCStatus== 1 ? 0 : 1); 
            sortBy = ascdesvCStatus ==0 ? 'ASC' : 'DESC';  
        System.debug('ascdesvCStatus : ' + ascdesvCStatus);
        search();
    }
    
    public Integer ascdesvCPriority{get; set;}
    public void sortByCasePriority()
    {
        sortName = 'Priority';
        ascdesvCPriority = ascdesvCPriority== -1  ? 0 : (ascdesvCPriority== 1 ? 0 : 1); 
            sortBy = ascdesvCPriority ==0 ? 'ASC' : 'DESC';  
        System.debug('ascdesvCPriority : ' + ascdesvCPriority);
        search();
    }
    
    public Integer ascdesvCAccount{get; set;}
    public void sortByCaseAccount()
    {
        sortName = 'Account.name';
        ascdesvCAccount = ascdesvCAccount== -1  ? 0 : (ascdesvCAccount== 1 ? 0 : 1); 
            sortBy = ascdesvCAccount ==0 ? 'ASC' : 'DESC';  
        System.debug('ascdesvCAccount : ' + ascdesvCAccount);
        search();
    }
    
    public Integer ascdesvRID{get; set;}
    public void sortByRallyID()
    {
        sortName = 'Rally_ID__c';
        ascdesvRID = ascdesvRID== -1  ? 0 : (ascdesvRID== 1 ? 0 : 1); 
            sortBy = ascdesvRID ==0 ? 'ASC' : 'DESC';  
        System.debug('ascdesvRID : ' + ascdesvRID);
        search();
    }
    
    public Integer ascdesvRDS{get; set;}
    public void sortByRallyDS()
    {
        sortName = 'RALLYSM__Development_Status__c';
        ascdesvRDS = ascdesvRDS== -1  ? 0 : (ascdesvRDS== 1 ? 0 : 1); 
            sortBy = ascdesvRDS ==0 ? 'ASC' : 'DESC';  
        System.debug('ascdesvRDS : ' + ascdesvRDS);
        search();
    }
    
    public List<Case> caseResults{get; set;}
    
    public list<Integer> pageOffsets{get; set;} 
    
       
    
    public void search(){
        
        system.debug('offsetCount :: ' + currentPageOffset);
                        
        if(caseStatus=='Open' && AcctName=='All')
        {
            caseResults = Database.query(
                'SELECT id,casenumber,subject,Owner.name,account.name,status,priority,Rally_ID__c,RALLYSM__Development_Status__c,Date_Swap_Triage_New__c,Severity__c,CaseHourCalc__c,DateTime_Swaped_In_Triage__c '+
                'FROM Case where Date_Swap_Triage_New__c !=null and status!=\'Closed\' '+
                'ORDER BY ' + sortname + ' ' + sortBy + ' ' +
                'limit 2 OFFSET ' + currentPageOffset
            );
            if(caseResults!=null && !caseResults.isEmpty())
            {
                recordCount = Database.countQuery(
                    'SELECT count() '+
                    'FROM Case where  Date_Swap_Triage_New__c !=null and status!=\'Closed\' '
                );                
            }
        }
        
        if(caseStatus=='Closed'  && AcctName=='All')
        {
            caseResults = Database.query(
                'SELECT id,casenumber,subject,Owner.name,account.name,status,priority,Rally_ID__c,RALLYSM__Development_Status__c,Date_Swap_Triage_New__c,Severity__c,CaseHourCalc__c,DateTime_Swaped_In_Triage__c '+
                'FROM Case where Date_Swap_Triage_New__c !=null and  status=\'Closed\' '+
                'ORDER BY ' + sortname + ' ' + sortBy + ' ' +
                'limit 2 OFFSET ' + currentPageOffset
            );        
            if(caseResults!=null && !caseResults.isEmpty())
            {
                recordCount = Database.countQuery(
                    'SELECT count() '+
                    'FROM Case where  Date_Swap_Triage_New__c !=null and  status=\'Closed\' '
                );                
            }
        }                                
        
        if(caseStatus=='Open'  && AcctName!='All')
        {
            caseResults = Database.query(
                'SELECT id,casenumber,subject,Owner.name,account.name,status,priority,Rally_ID__c,RALLYSM__Development_Status__c,Date_Swap_Triage_New__c,Severity__c,CaseHourCalc__c,DateTime_Swaped_In_Triage__c '+
                'FROM Case where Date_Swap_Triage_New__c !=null and status!=\'Closed\' and account.name=\''+ AcctName + '\' ' +
                'ORDER BY ' + sortname + ' ' + sortBy + ' ' +
                'limit 2 OFFSET ' + currentPageOffset
            );
            
            if(caseResults!=null && !caseResults.isEmpty())
            {
                recordCount = Database.countQuery(
                    'SELECT count() '+
                    'FROM Case where  Date_Swap_Triage_New__c !=null and  status!=\'Closed\' and account.name=\''+ AcctName + '\' '
                );                
            }                
        }
        
        if(caseStatus=='Closed'  && AcctName!='All')
        {
            caseResults = Database.query(
                'SELECT id,casenumber,subject,Owner.name,account.name,status,priority,Rally_ID__c,RALLYSM__Development_Status__c,Date_Swap_Triage_New__c,Severity__c,CaseHourCalc__c,DateTime_Swaped_In_Triage__c '+
                'FROM Case where Date_Swap_Triage_New__c !=null and status=\'Closed\' and account.name=\''+ AcctName + '\' ' +
                'ORDER BY ' + sortname + ' ' + sortBy + ' ' +
                'limit 2 OFFSET ' + currentPageOffset
            );
            if(caseResults!=null && !caseResults.isEmpty())
            {
                recordCount = Database.countQuery(
                    'SELECT count() '+
                    'FROM Case where  Date_Swap_Triage_New__c !=null and  status=\'Closed\' and account.name=\''+ AcctName + '\' '
                );                
            } 
              
        }                
        
        previousPageOffset = (currentPageOffset==0) ? 0 : (currentPageOffset -2);
            nextPageOffset = ((currentPageOffset + 2) > recordCount) ? (currentPageOffset + (Math.mod(recordCount, 2))) : (currentPageOffset + 2) ;
                System.debug('previousPageOffset : ' + previousPageOffset);        
        System.debug('nextPageOffset : ' + nextPageOffset);  
              
        
    }
   
}