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
affuaffu 

Task List Controller??????

Hai All,

 

I have one requirement, can we the get the Task list in Visualforce page?????

 

I have tried a lot but i couldnt find out the solution.

Here is the code. Im getting error as List Controllers are supported for Task

public with sharing class TaskPagination {
private final Task t;

public TaskPagination(ApexPages.StandardSetController controller) {
this.c = (Task)controller.getRecord();
}
public ApexPages.StandardSetController TaskRecords{
get {
if(TaskRecords == null) {
return new ApexPages.StandardSetController(Database.getQueryLocator(
[SELECT t.ActivityDate, t.Status FROM Task t]));
}
return TaskRecords;
}
private set;
}
public List<Task> getTaskPagination() {
return (List<Task>) TaskRecords.getRecords();
}
}

 Thanks in Advance..........

 

Best Answer chosen by Admin (Salesforce Developers) 
S91084S91084

Hi,

 

Just use the following code :

 

public with sharing class TaskListController {

    private integer counter=0;  //keeps track of the offset
    private integer list_size=20; //sets the page size or number of rows
    public integer total_size; //used to show user the total size of the list
        
    public string selectedPage{get;set{selectedPage=value;}
    }
    
    public TaskListController() {
        total_size = [select count() from Task]; //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, Status, Description
                                     from Task 
                                     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 refreshGrid() { //user clicked a page number        
        system.debug('**** ' + selectedPage);
        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 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);
        }
    }
    
    public static testMethod void testTaskListController() {
        //just a little bit of code coverage to get you by :-)
        TaskListController t = new TaskListController();        
        t.getTasks();    
        t.getDisableNext();
        t.getDisablePrevious();
        t.getTotal_size();
        t.Next();
        t.Next();
        t.Previous();  
        t.End();    
        t.getMyCommandButtons();
        t.SelectedPage='10';
        t.total_size=50;
        t.refreshGrid();        
        t.getMyCommandButtons(); 
        t.getPageNumber();
        t.getTotalPages();                       
    }    
}

 

 

<apex:page controller="TaskListController">
    <apex:form >
        <apex:pageBlock >               
            <apex:actionFunction action="{!refreshGrid}" name="queryByPage" reRender="myPanel,myButtons" >
                <apex:param name="firstParam" assignTo="{!selectedPage}" value="" />
            </apex:actionFunction>
            
            <apex:dynamicComponent componentValue="{!myCommandButtons}"/>        
            
            <apex:pageBlockSection title="Task List (Total List Size: {!total_size})" collapsible="false">
            <apex:outputPanel id="myPanel">
            <apex:pageMessages id="theMessages" />
            <apex:pageBlockTable value="{!tasks}" var="t" align="center">
                <apex:column value="{!t.Subject}" />
                <apex:column value="{!t.Status}" />
                <apex:column value="{!t.Description}"/>
                <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
            </apex:pageBlockTable>
            </apex:outputPanel>
            </apex:pageBlockSection>    
        </apex:pageBlock>
    </apex:form>
</apex:page>

 If you have to display additional task fields and filters , just add them to the query.

 

Hope this is what you are looking.

All Answers

SFDC_VikashSFDC_Vikash

Hi

 

This is not possible to set list controller for task , it is still not supported by salesforce. All you can do , you have to add custom controller for this in which you can code as per your requirement.

affuaffu

Thanks for replying ...

I have already chkd the code and i dint understand how it works and it is complicated one

can u please suggest any other solution will be very helpful to me.

 

 

Thanku U.

S91084S91084

Hi,

 

Just use the following code :

 

public with sharing class TaskListController {

    private integer counter=0;  //keeps track of the offset
    private integer list_size=20; //sets the page size or number of rows
    public integer total_size; //used to show user the total size of the list
        
    public string selectedPage{get;set{selectedPage=value;}
    }
    
    public TaskListController() {
        total_size = [select count() from Task]; //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, Status, Description
                                     from Task 
                                     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 refreshGrid() { //user clicked a page number        
        system.debug('**** ' + selectedPage);
        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 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);
        }
    }
    
    public static testMethod void testTaskListController() {
        //just a little bit of code coverage to get you by :-)
        TaskListController t = new TaskListController();        
        t.getTasks();    
        t.getDisableNext();
        t.getDisablePrevious();
        t.getTotal_size();
        t.Next();
        t.Next();
        t.Previous();  
        t.End();    
        t.getMyCommandButtons();
        t.SelectedPage='10';
        t.total_size=50;
        t.refreshGrid();        
        t.getMyCommandButtons(); 
        t.getPageNumber();
        t.getTotalPages();                       
    }    
}

 

 

<apex:page controller="TaskListController">
    <apex:form >
        <apex:pageBlock >               
            <apex:actionFunction action="{!refreshGrid}" name="queryByPage" reRender="myPanel,myButtons" >
                <apex:param name="firstParam" assignTo="{!selectedPage}" value="" />
            </apex:actionFunction>
            
            <apex:dynamicComponent componentValue="{!myCommandButtons}"/>        
            
            <apex:pageBlockSection title="Task List (Total List Size: {!total_size})" collapsible="false">
            <apex:outputPanel id="myPanel">
            <apex:pageMessages id="theMessages" />
            <apex:pageBlockTable value="{!tasks}" var="t" align="center">
                <apex:column value="{!t.Subject}" />
                <apex:column value="{!t.Status}" />
                <apex:column value="{!t.Description}"/>
                <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
            </apex:pageBlockTable>
            </apex:outputPanel>
            </apex:pageBlockSection>    
        </apex:pageBlock>
    </apex:form>
</apex:page>

 If you have to display additional task fields and filters , just add them to the query.

 

Hope this is what you are looking.

This was selected as the best answer
affuaffu

Thanku vry much......  :)

Pavan Kumar.ax1724Pavan Kumar.ax1724

Hi,

 

thanks for that code.. But due to some reason.. I cannot see that visual force page in the Dashboard Datasources.

 

Please do help me out in rectifying the issue.

 

Any help would be highly appreciable.

 

Regards,

Pavan Kumar Garlapati

Shephali SwarnkarShephali Swarnkar
Hi,

    I am getting this error " [Error] Error: Compile Error: Test methods must be in test classes at line 174 column 35 "
Please help.
Shephali SwarnkarShephali Swarnkar
Hi All,
        @S91084,   The code you have provided here is working well but can we make it Dynmic as in this code u have set the List_Size=20.

Please reply.

Kind Regards
Shephali
 
Amit SAmit S
@Shephali,

You seem to have recently worked on this peice of code. I am working on a requirement to create a LTA field on Task. I have created the Custom Object, LTA Field and displayed using the VF page.

But, I have to display this VF page selectively for a particular User Profile & Page Layout.

I tried creating a Custom button on tasks and displaying the button on the Account's Task releated list. But here I am getting the error "List controllers are not supported for Task" as I tried to display the tasks.

Can you help me with a solution around this?
Shephali SwarnkarShephali Swarnkar
@Amit S
    
            Sure will help.Please tell me.
Amit SAmit S
Thanks Shephali, I customized the "New Task" Button for the same purpose instead of the "Log a Call" button.