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
Eric FortenberryEric Fortenberry 

How to sort Tasks Ascending by ActivityDate

How would I sort AllOpenTasks and AllCompletedTasks ascending by ActivityDate?

 

AccountExt Class:

public class AccountExt {

    public AccountExt(ApexPages.StandardController controller) {
    acc = (Account) controller.getRecord();
    }
    private final Account acc;
    Set<id> AccountIds = new set<ID>();
    
    public void AccountIds(){
    	AccountIds.add(acc.Id);
        for(integer i=0;i<5;i++){
            for(Account a: [select Id from Account where ParentId in :AccountIds limit 45000])
            AccountIds.add(a.Id);
        }
    }
    
    public list<Contact> getAllContacts(){ 
        if(AccountIds.size()==0) AccountIds();
        return [select Name, Title, Account.Name, Phone, Email from Contact where AccountId in :AccountIds];
        }
	public list<Task> getAllOpenTasks(){ 
        if(AccountIds.size()==0) AccountIds();
        list<Task> tasks = [select Id, Subject, Type, Call_Notes__c, Account.Name, WhoID, ActivityDate, OwnerId from Task where AccountId in :AccountIds and IsClosed = False];
        return tasks;
        }
    public list<Task> getAllCompletedTasks(){ 
        if(AccountIds.size()==0) AccountIds();
        list<Task> tasks = [select Subject, Type, Call_Notes__c, Account.Name, WhoId, ActivityDate, OwnerId, Id from Task where AccountId in :AccountIds and Isclosed = true];
        return tasks;
        }
}

 

AllOpenTasks Visualforce Page:

<apex:page standardController="Account" extensions="AccountExt" sidebar="false" showHeader="false">
    <script>
      function doRedirect()
      {
       window.parent.location.href = '{!URLFOR($Action.Task.NewTask)}';
     }
    </script>
    <apex:form >
    <apex:pageBlock title="Open Tasks">
        <apex:pageBlockButtons location="top">
            <apex:commandbutton onClick="doRedirect()" value="New Task"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!AllOpenTasks}" var="item">
            <apex:column headervalue="Subject">
                <apex:outputLink value="/{!item.Id}" target="_parent"><apex:outputField value="{!item.Subject}" /></apex:outputLink>
            </apex:column>
            <apex:column value="{!item.Type}" />
            <apex:column value="{!item.Call_Notes__c}" />
            <apex:column value="{!item.Account.Name}" />            
            <apex:column value="{!item.WhoId}" />
            <apex:column value="{!item.ActivityDate}" />
            <apex:column value="{!item.OwnerId}" />
        </apex:pageBlockTable>
    </apex:pageBlock>  
    </apex:form>
</apex:page>

 

AllCompletedTasks VisualForce Page:

<apex:page standardController="Account" extensions="AccountExt" sidebar="false" showHeader="false">
    <script>
      function doRedirect()
      {
       window.parent.location.href = '{!URLFOR($Action.Task.NewTask)}';
     }
    </script>
    <apex:form >
    <apex:pageBlock title="Open Tasks">
        <apex:pageBlockButtons location="top">
            <apex:commandbutton onClick="doRedirect()" value="New Task"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!AllOpenTasks}" var="item">
            <apex:column headervalue="Subject">
                <apex:outputLink value="/{!item.Id}" target="_parent"><apex:outputField value="{!item.Subject}" /></apex:outputLink>
            </apex:column>
            <apex:column value="{!item.Type}" />
            <apex:column value="{!item.Call_Notes__c}" />
            <apex:column value="{!item.Account.Name}" />            
            <apex:column value="{!item.WhoId}" />
            <apex:column value="{!item.ActivityDate}" />
            <apex:column value="{!item.OwnerId}" />
        </apex:pageBlockTable>
    </apex:pageBlock>  
    </apex:form>
</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
Paul.FoxPaul.Fox

You probably also want NULLS LAST, so it'll be Order by ActivityDate ASC NULLS LAST

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

In order to sort, you can make use of "order by" clause in your SOQL.

 

For more detail follow the below link

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_orderby.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

b-Forceb-Force
list<Task> tasks = [select Id, Subject, Type, Call_Notes__c, Account.Name, WhoID, ActivityDate, OwnerId from Task where AccountId in :AccountIds and IsClosed = False order By ActivityDate ASC];

 

Paul.FoxPaul.Fox

You probably also want NULLS LAST, so it'll be Order by ActivityDate ASC NULLS LAST

This was selected as the best answer