+ Start a Discussion
Amit Yadav 9Amit Yadav 9 

how to order one table with multiple order conditions?

hi,

i need to order Tasks with a High priority  display first in red font than sort by Due Date with the oldest due date first.

any help will be much appreciated. thank you
Best Answer chosen by Amit Yadav 9
Sunil MadanaSunil Madana
Hi, Not sure if the below code helps.

Apex-Class:
public class myTaskClassComponent {
    public List<Task> ListTasks {get; set;}
    public void fetchRecords(){
        ListTasks = [SELECT DB_Activity_Type__c,ActivityDate,Priority,Status,Subject,TaskSubtype FROM Task ORDER BY Priority DESC, ActivityDate DESC];
    }
}

VF-Page:
<apex:page controller="myTaskClassComponent">
    <apex:form id="VF_AcctsForm">
        <apex:pageBlock>
            <apex:commandButton value="List Records" action="{!fetchRecords}" rerender="VF_AcctsFormPageBlockTbl"/>
            <apex:pageBlockTable value="{!ListTasks}" var="w" id="VF_AcctsFormPageBlockTbl">                
                <apex:column headervalue="Activity Type" id="VF_AcctsFormPageBlockTblhdr2">
                    <apex:outputtext value="{!w.DB_Activity_Type__c}" id="VF_AcctsForminputField2"/>
                </apex:column>
                <apex:column headervalue="Due date" id="VF_AcctsFormPageBlockTblhdr3">
                    <apex:outputText value="{0, date, d-MMM-yyyy}" id="VF_AcctsForminputField3">
                        <apex:param value="{!w.ActivityDate}" />
                    </apex:outputText>
                </apex:column>
                <apex:column headerValue="Priority" id="VF_AcctsFormPageBlockTblhdr4">
                    <apex:outputtext value="{!w.Priority}" id="VF_AcctsForminputField4"/>
                </apex:column>
                <apex:column headerValue="Status" id="VF_AcctsFormPageBlockTblhdr5">
                    <apex:outputtext value="{!w.Status}" id="VF_AcctsForminputField5"/>
                </apex:column>
                <apex:column headerValue="Subject" id="VF_AcctsFormPageBlockTblhdr6">
                    <apex:outputtext value="{!w.Subject}" id="VF_AcctsForminputField6"/>
                </apex:column>
                <apex:column headerValue="Task Sub-Type" id="VF_AcctsFormPageBlockTblhdr7">
                    <apex:outputtext value="{!w.TaskSubtype}" id="VF_AcctsForminputField7"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

If the above suggestion(s) worked, let us know by marking the answer as "Best Answer" right under the comment which will help the rest of the community should they have a similar issue in the future.
Thanks, Sunil

All Answers

Sunil MadanaSunil Madana
Hi, Not sure if the below code helps.

Apex-Class:
public class myTaskClassComponent {
    public List<Task> ListTasks {get; set;}
    public void fetchRecords(){
        ListTasks = [SELECT DB_Activity_Type__c,ActivityDate,Priority,Status,Subject,TaskSubtype FROM Task ORDER BY Priority DESC, ActivityDate DESC];
    }
}

VF-Page:
<apex:page controller="myTaskClassComponent">
    <apex:form id="VF_AcctsForm">
        <apex:pageBlock>
            <apex:commandButton value="List Records" action="{!fetchRecords}" rerender="VF_AcctsFormPageBlockTbl"/>
            <apex:pageBlockTable value="{!ListTasks}" var="w" id="VF_AcctsFormPageBlockTbl">                
                <apex:column headervalue="Activity Type" id="VF_AcctsFormPageBlockTblhdr2">
                    <apex:outputtext value="{!w.DB_Activity_Type__c}" id="VF_AcctsForminputField2"/>
                </apex:column>
                <apex:column headervalue="Due date" id="VF_AcctsFormPageBlockTblhdr3">
                    <apex:outputText value="{0, date, d-MMM-yyyy}" id="VF_AcctsForminputField3">
                        <apex:param value="{!w.ActivityDate}" />
                    </apex:outputText>
                </apex:column>
                <apex:column headerValue="Priority" id="VF_AcctsFormPageBlockTblhdr4">
                    <apex:outputtext value="{!w.Priority}" id="VF_AcctsForminputField4"/>
                </apex:column>
                <apex:column headerValue="Status" id="VF_AcctsFormPageBlockTblhdr5">
                    <apex:outputtext value="{!w.Status}" id="VF_AcctsForminputField5"/>
                </apex:column>
                <apex:column headerValue="Subject" id="VF_AcctsFormPageBlockTblhdr6">
                    <apex:outputtext value="{!w.Subject}" id="VF_AcctsForminputField6"/>
                </apex:column>
                <apex:column headerValue="Task Sub-Type" id="VF_AcctsFormPageBlockTblhdr7">
                    <apex:outputtext value="{!w.TaskSubtype}" id="VF_AcctsForminputField7"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

If the above suggestion(s) worked, let us know by marking the answer as "Best Answer" right under the comment which will help the rest of the community should they have a similar issue in the future.
Thanks, Sunil
This was selected as the best answer
Amit Yadav 9Amit Yadav 9
yes it worked like a charm,. Thank you  sunil

Amit Yadav
Amit Yadav 9Amit Yadav 9
hi,
just one more question.

How can i add fields of a custom object in the same table based on whatid of these tasks.????
thanks, Amit Yadav
Sunil MadanaSunil Madana
Hi, If you fetch the whatId from another query then include it in the query before "ORDER BY".

Say you have customer Object "Student__c" which has field "Student_ID", then,

Case-I
public class myTaskClassComponent {
    public List<Task> ListTasks {get; set;}
    public void fetchRecords() {
        String whatID = '90999XXXXXX'; // for example
        ListTasks = [SELECT DB_Activity_Type__c,ActivityDate,Priority,Status,Subject,TaskSubtype FROM Task where whatId =: whatID ORDER BY Priority DESC, ActivityDate DESC];
    }
}
Case-II
public class myTaskClassComponent {
    public List<Task> ListTasks {get; set;}
    public List<Student__c> ListStudents {get; set;}
    public void fetchRecords(){
        for(Student__c LstRecords: ListStudents) {
              ListTasks = [SELECT DB_Activity_Type__c,ActivityDate,Priority,Status,Subject,TaskSubtype FROM Task where whatId=: LstRecords.whatId ORDER BY Priority DESC, ActivityDate DESC];
        }
    }
}
Hope the above idea helps.