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
Zach AckermanZach Ackerman 

Pagination Class for Tasks

I have the following pagination code/vf page. I am getting an error that says 'List controllers are not supported for Task '. How can I update the code to make the visualforce page work?

Apex: 
public class PaginationTaskHomePage1{


public String soql {get;set;}
public List <Task> TaskList1 = New List <Task>();
public String soqlsort {get;set;}
public List <Task> TaskList2= New List <Task>();
Public Integer noOfRecords{get; set;}
Public Integer size{get;set;}
            public List <Task> getTaskList()
            {
            return con.getRecords();
            }
            
            public Apexpages.StandardSetController con{
            get{
                    if(con==null){
                 size=500; 
                string OwnersId =  userinfo.getUserId() ; 
                string type1 =   'Lost NB Round 1';
                string type2 = 'Lost Renewals';
                    soqlsort='Select Subject,WhoID,WhatId,OwnerId,Opportunity_Effective_Date__c,Primary_Medical_Effective_Date__c,Type,CreatedDate From Task Where OwnerId = :OwnersId AND (Type = :type1 OR Type= :type2)';   
                    
                    TaskList1 = Database.query(soqlsort + ' Order by ' +  sortField +' ' + sortDir);
                    
                    con= new ApexPages.StandardSetController(TaskList1);
                    con.setPageSize(size);
                    noOfRecords=Con.getResultSize();
                  }
                  return con;
             }
             set;
             }
             public Boolean hasNext {
             
             get {
                 return con.getHasNext();
                 }
                 set;
                 
                 }
                 
                 //indicates whether therre are more records before the current page set. 
                 
                 public Boolean hasPrevious{
                 get{
                 return con.getHasPrevious();
                 
                 }
                 set;
                 }
                 
            public Integer pageNumber {
            get{
            return con.getPageNumber();
            }
            set;
            }
            
            public void next(){
            con.next();
            
            }
            //returns the PageRerfernce of the original page, if known, or home page. 
            public void cancel() {
            con.cancel();
            }
            // Method for Constructor is used for Test Class
            public PaginationTaskHomePage1()
            {
            }
            //Toggles the sorting of query from asc to desc
            
            public void toggleSort() {
            
            //simply toggle the direction
            sortDir= sortDir.equals('asc') ? 'desc' : 'asc';
            
            //run the query again for sorting other columns 
                     string OwnersId =  userinfo.getUserId() ;
               
                string type1 =   'Lost NB Round 1';
                string type2 = 'Lost Renewals';
            soqlsort = 'Select Subject,WhoID,WhatId,OwnerId,Opportunity_Effective_Date__c,Primary_Medical_Effective_Date__c,Type,CreatedDate From Task Where OwnerId = :OwnersId AND (Type = :type1 OR Type= :type2)';      
                    
            
            //adding string array to a List array
            TaskList2 = Database.query(soqlsort + ' Order by ' +  sortField + ' ' + sortDir);
            
            // Adding Caselist to standard Pagination controller variable
             con= new ApexPages.StandardSetController(TaskList2);
            
            //Set Page Size to 500
            con.setPageSize(500);
            }
            //the current sort direction defaults to asc
            
            
            public String sortDir{
            
            
            
            //To set a direction either in ascending order or descending order. 
            
            
            
            
                get{if(sortDir==null){sortDir = 'asc';} return sortDir;}
                set;
                }
                //the current field to sort by. defaults to probability
                
                public String sortField{
               //to set a field sorting.
                    get{if (sortField==null){sortField='Opportunity_Effective_Date__c';} return sortField;}
                    set;
                    }
                    
                    
                    
                    
                        public pageReference refresh() {
                       Con = null;
                       getTaskList();
                       Con.setPageNumber(1);
                       return null; 
    }
    
    
                    }

VisualForce:
 
<apex:page controller="PaginationTaskHomePage1" tabstyle="Task">
    <apex:form >
       <apex:pageBlock id="pb">
       <apex:sectionHeader title="{!$User.FirstName}'s Lost Renewal & New Business List"/>
       <apex:commandButton value="New Opportunity" onClick="window.open('https://cs15.salesforce.com/006/e?retURL=%2F006%2Fo&RecordType=01240000000Uexl&ent=Opportunity');"/>
       <apex:commandButton value="Detailed Report" onClick="window.open('https://cs15.salesforce.com/00Oe0000000fRSG');"/>
       
            <apex:pageBlockTable value="{!TaskList}" var="o" id="myTable">
         <apex:column >
        <apex:facet name="header">
            <apex:commandLink value="Subject" action="{!toggleSort}" reRender="pb">
                <apex:param name="sortField" value="Subject" assignTo="{!sortField}" />
            </apex:commandLink>
        </apex:facet>
        <apex:outputlink value="/{!o.subject}" target="__blank">{!o.subject}</apex:outputlink> 
    </apex:column>
    <apex:column >
        <apex:facet name="header">
            <apex:commandLink value="WhoID" action="{!toggleSort}" reRender="pb">
                <apex:param name="sortField" value="WhoId" assignTo="{!sortField}" />
            </apex:commandLink>
        </apex:facet>
        <apex:outputField value="{!o.WhoId}" />
    </apex:column>
    <apex:column >
     <apex:facet name="header">
            <apex:commandLink value="WhatId" action="{!toggleSort}" reRender="pb">
                <apex:param name="sortField" value="WhatId" assignTo="{!sortField}" />
            </apex:commandLink>
        </apex:facet>
        <apex:outputField value="{!o.WhatId}" />
    </apex:column>
    <apex:column >
        <apex:facet name="header">
            <apex:commandLink value="OwnerId" action="{!toggleSort}" reRender="pb">
                <apex:param name="sortField" value="OwnerId" assignTo="{!sortField}" />
            </apex:commandLink>
        </apex:facet>
        <apex:outputlink value="/{!o.OwnerId}" target="__blank">{!o.OwnerId}</apex:outputlink> 
    </apex:column>
 <apex:column >
     <apex:facet name="header">
            <apex:commandLink value="Opportunity_Effective_Date__c" action="{!toggleSort}" reRender="pb">
                <apex:param name="sortField" value="Opportunity_Effective_Date__c" assignTo="{!sortField}" />
            </apex:commandLink>
        </apex:facet>
        <apex:outputField value="{!o.Opportunity_Effective_Date__c}" />
    </apex:column>
    <apex:column >
     <apex:facet name="header">
            <apex:commandLink value="Primary_Medical_Effective_Date__c" action="{!toggleSort}" reRender="pb">
                <apex:param name="sortField" value="Primary_Medical_Effective_Date__c" assignTo="{!sortField}" />
            </apex:commandLink>
        </apex:facet>
        <apex:outputField value="{!o.Primary_Medical_Effective_Date__c}" />
    </apex:column>
    <apex:column >
     <apex:facet name="header">
            <apex:commandLink value="Type" action="{!toggleSort}" reRender="pb">
                <apex:param name="sortField" value="Type" assignTo="{!sortField}" />
            </apex:commandLink>
        </apex:facet>
        <apex:outputField value="{!o.Type}" />
    </apex:column>
    <apex:column >
     <apex:facet name="header">
            <apex:commandLink value="Create Date" action="{!toggleSort}" reRender="pb">
                <apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}" />
            </apex:commandLink>
        </apex:facet>
        <apex:outputField value="{!o.CreatedDate}" />
    </apex:column>   
</apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!Con.first}" disabled="{!!Con.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!Con.previous}" disabled="{!!Con.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!Con.next}" disabled="{!!Con.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!Con.last}" disabled="{!!Con.hasNext}" title="Last Page"/>
                <apex:outputText >{!(Con.pageNumber * size)+1-size}-{!IF((Con.pageNumber * size)>noOfRecords, noOfRecords,(Con.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                  <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                   </apex:outputPanel> 
            </apex:panelGrid>
            </apex:pageBlock>
    </apex:form>
</apex:page>

 
karthikeyan perumalkarthikeyan perumal
Hello, 

I'm guessing this is down to some of the bespoke funcitonality inculded in order items around saving and discount calculations etc..
I think you're going to have to implement pagination manually, you could either query everything and do paging, or use the SOQL OFFSET keyword to pull different pages of data from the DB.
One other alternative is to send all items through to the page and do it through a jQuery plugin or similar, though you'd want to be sure you're not sending hundreds of records back and forth for performance reasons (this does seem unlikely but it'll depend on the business).

plesae find the reference link for using offset in SOQL query 

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_offset.htm

hope this will helps you. 

Thanks
karthik
 
Vinuthh SVinuthh S
Hi Zach

https://hisrinu.wordpress.com/2012/01/09/pagination-using-standardsetcontroller/

Check this Link for the pagination using the standard set controller.

Thanks
Vinuthh S
Zach AckermanZach Ackerman
The code above works for Accounts, Opportunities, etc. The problem is it doesn't work for Tasks. 
Vinuthh SVinuthh S
https://developer.salesforce.com/forums/?id=906F000000098QYIAY

Check this link
Zach AckermanZach Ackerman
I found this link previously I was able to adapt the code to my needs. However, I'm having trouble adding the sort function. I want the users to be able to click on the column header and sort the table. 

apex
 
public with sharing class TaskListController2 {

    private integer counter=0;  //keeps track of the offset
    private integer list_size=100; //sets the page size or number of rows
    public integer total_size; //used to show user the total size of the list
    string OwnersId =  userinfo.getUserId() ; 

        
    public string selectedPage{get;set{selectedPage=value;}
    }
    
    public TaskListController2() {
        total_size = [select count() from Task WHERE OwnerId=:OwnersId AND (type<>'Lost NB Round 1' OR type<>'Lost Renewals')AND Status<>'Completed']; //set the total size in the constructor
        selectedPage='0';
    }
    
    public Task[] getTasks() {
                
        if (selectedPage != '0') counter = list_size*integer.valueOf(selectedPage)-list_size;
        try { //we have to catch query exceptions in case the list is greater than 2000 rows
                Task[] taskList = [select Id,Subject,WhoID,WhatId,OwnerId,Opportunity_Effective_Date__c,Primary_Medical_Effective_Date__c,Type,CreatedDate,Status 
                                     from Task 
                                     where OwnerId=:OwnersId AND (type!='Lost NB Round 1' OR type!='Lost Renewals') AND Status<>'Completed'
                                     order by Id
                                     limit :list_size
                                    offset :counter];                   
                return taskList;
        
        } catch (QueryException e) {                            
                ApexPages.addMessages(e);                   
                return null;
        }        
    }
    
    public Component.Apex.pageBlockButtons getMyCommandButtons() {
        
        //the reRender attribute is a set NOT a string
        Set<string> theSet = new Set<string>();
        theSet.add('myPanel');
        theSet.add('myButtons');
                
        integer totalPages;
        if (math.mod(total_size, list_size) > 0) {
            totalPages = total_size/list_size + 1;
        } else {
            totalPages = (total_size/list_size);
        }
        
        integer currentPage;        
        if (selectedPage == '0') {
            currentPage = counter/list_size + 1;
        } else {
            currentPage = integer.valueOf(selectedPage);
        }
     
        Component.Apex.pageBlockButtons pbButtons = new Component.Apex.pageBlockButtons();        
        pbButtons.location = 'top';
        pbButtons.id = 'myPBButtons';
        
        Component.Apex.outputPanel opPanel = new Component.Apex.outputPanel();
        opPanel.id = 'myButtons';
                                
        //the Previous button will alway be displayed
        Component.Apex.commandButton b1 = new Component.Apex.commandButton();
        b1.expressions.action = '{!Previous}';
        b1.title = 'Previous';
        b1.value = 'Previous';
        b1.expressions.disabled = '{!disablePrevious}';        
        b1.reRender = theSet;

        opPanel.childComponents.add(b1);        
                        
        for (integer i=0;i<totalPages;i++) {
            Component.Apex.commandButton btn = new Component.Apex.commandButton();
            
            if (i+1==1) {
                btn.title = 'First Page';
                btn.value = 'First Page';
                btn.rendered = true;                                        
            } else if (i+1==totalPages) {
                btn.title = 'Last Page';
                btn.value = 'Last Page';
                btn.rendered = true;                            
            } else {
                btn.title = 'Page ' + string.valueOf(i+1) + ' ';
                btn.value = ' ' + string.valueOf(i+1) + ' ';
                btn.rendered = false;             
            }
            
            if (   (i+1 <= 5 && currentPage < 5)
                || (i+1 >= totalPages-4 && currentPage > totalPages-4)
                || (i+1 >= currentPage-2 && i+1 <= currentPage+2))
            {
                btn.rendered = true;
            }
                                     
            if (i+1==currentPage) {
                btn.disabled = true; 
                btn.style = 'color:blue;';
            }  
            
            btn.onclick = 'queryByPage(\''+string.valueOf(i+1)+'\');return false;';
                 
            opPanel.childComponents.add(btn);
            
            if (i+1 == 1 || i+1 == totalPages-1) { //put text after page 1 and before last page
                Component.Apex.outputText text = new Component.Apex.outputText();
                text.value = '...';        
                opPanel.childComponents.add(text);
            } 
             
        }
        
        //the Next button will alway be displayed
        Component.Apex.commandButton b2 = new Component.Apex.commandButton();
        b2.expressions.action = '{!Next}';
        b2.title = 'Next';
        b2.value = 'Next';
        b2.expressions.disabled = '{!disableNext}';        
        b2.reRender = theSet;
        opPanel.childComponents.add(b2);
                
        //add all buttons as children of the outputPanel                
        pbButtons.childComponents.add(opPanel);  
  
        return pbButtons;

    }
      public PageReference Start() { //user clicked start
        selectedPage = '0';
        counter = 0;
        return null;
        }

    
    public PageReference Previous() { //user clicked previous button
        selectedPage = '0';
        counter -= list_size;
        return null;
    }

    public PageReference Next() { //user clicked next button
        selectedPage = '0';
        counter += list_size;
        return null;
    }

    public PageReference End() { //user clicked end
        selectedPage = '0';
        counter = total_size - math.mod(total_size, list_size);
        return null;
   
    }
    
    public Boolean getDisablePrevious() { //this will disable the previous and beginning buttons
        if (counter>0) return false; else return true;
    }

    public Boolean getDisableNext() { //this will disable the next and end buttons
        if (counter + list_size < total_size) return false; else return true;
    }
    public pagereference refresh(){
      selectedpage='0';
      getTotal_Size();
    return Null; 
    }
    public Integer getTotal_size() {
        return total_size;
    }
    
    public Integer getPageNumber() {
        return counter/list_size + 1;
    }

    public Integer getTotalPages() {
        if (math.mod(total_size, list_size) > 0) {
            return total_size/list_size + 1;
        } else {
            return (total_size/list_size);
        }
        }
 }



VF
 
<apex:page controller="TaskListController2" tabstyle="Task">
    <apex:form >
        <apex:pageBlock id="pb">
       <apex:sectionHeader title="{!$User.FirstName}'s Active Group Task List"/>
       <apex:commandButton value="New Task" onClick="window.open('https://cs15.salesforce.com/00T/e?retURL=%2Fhome%2Fhome.jsp');"/>
       <apex:commandButton value="Detailed Report" onClick="window.open('https://cs15.salesforce.com/00Oe0000000fT1g');"/>                  
                 
           
            <apex:outputPanel id="myPanel">
            <apex:pageMessages id="theMessages" />
            <apex:pageBlockTable value="{!tasks}" var="t" align="center">
                <apex:column headervalue="Subject" >
                <apex:outputlink value="/{!t.id}" target="__blank">{!t.Subject}</apex:outputlink> 
                </apex:column>
                <apex:column value="{!t.WhoId}" />
                 <apex:column headervalue="Related To" >
                <apex:outputlink value="/{!t.WhatId}" target="__blank">{!t.Whatid}</apex:outputlink> 
                </apex:column>
                <apex:column value="{!t.Opportunity_Effective_Date__c}"/>
                <apex:column value="{!t.Primary_Medical_Effective_Date__c}"/>
                <apex:column value="{!t.Type}"/>
                <apex:column value="{!t.CreatedDate}"/>
                <apex:column value="{!t.Status}"/>
            </apex:pageblocktable>  
       <apex:panelGrid columns="7">
       <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!Start}" disabled="{!disablePrevious}" title="First Page"/>
       <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!previous}" disabled="{!disablePrevious}" title="Previous Page"/>
       <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!next}" disabled="{!disableNext}" title="Next Page"/>
       <apex:commandButton status="fetchStatus" reRender="pb" value="|>" action="{!end}" disabled="{!disableNext}" title="Last Page"/>
       <apex:outputText > {!pageNumber}- {!total_size} of {!total_size}</apex:outputText>
       <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!Refresh}" title="Refresh Page"/>
       <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                   </apex:outputPanel> 
    </apex:panelGrid>
             </apex:outputPanel> 
        </apex:pageBlock>
    </apex:form>
</apex:page>