Find your content:

Search form

You are here

How do I sort cases using a pageBlockTable?

 
Share

I have a Visualforcepage with an apex:pageBlocKTable displaying Cases. I am trying to add the ability to sort the cases by CaseNumber. I looked over this wiki page: http://wiki.developerforce.com/page/Sorting_Tables and implemented it, but I am getting an error.

This is my Visualforce page:

<apex:page standardController="Case" extensions="caseSort" recordSetVar="Case" sidebar="true" showHeader="true">
    <apex:form >
        <apex:pageBlock title="Cases">
                <apex:pageBlockTable value="{!case}" var="c" rows="50" id="cases_table" >
                    <apex:column >
                        <a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.CaseNumber}</a>
                        <apex:facet name="header">
                            <apex:commandLink value="{!$ObjectType.Case.Fields.CaseNumber.Label}" action="{!doSort}" rerender="table">
                                <apex:param name="sortField" value="CaseNumber" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                    </apex:column>
                    <apex:column value="{!c.ContactId}" />
                    <apex:column >
                        <a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.Subject}</a>
                        <apex:facet name="header">Subject</apex:facet>
                    </apex:column>
                    <apex:column value="{!c.Status}" />
                    <apex:column value="{!c.Priority}" />
                    <apex:column value="{!c.CreatedDate}" />
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    <base target="_parent"/>
</apex:page>

and this is my controller:

public class caseSort {

    public caseSort(ApexPages.StandardSetController controller) {

    }

    List<Case> cases;
    public String sortField {get; set;}
    public String previousSortField {get; set;}

    public List<Case> getCases() {
        if(cases == null){
            cases = [select CaseNumber, ContactId, Subject, Status, Priority, CreatedDate from Case];
        }
        return cases;
    }

    public void doSort(){
        String order = 'asc';

        /*This checks to see if the same header was click two times in a row, if so 
        it switches the order.*/
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }

        //To sort the table we simply need to use this one line, nice!
        superSort.sortList(cases,sortField,order);
    }
}

When I go to the page and click the CaseNumber header to sort, I get this error:

Attempt to de-reference a null object
Error is in expression '{!doSort}' in component <apex:page> in page casestestpage

An unexpected error has occurred. Your development organization has been notified.

What am I doing wrong, and what do I have to do to fix it? Thanks!


Attribution to: Di Zou

Possible Suggestion/Solution #1

Are you trying to have it sort by default -- or upon click?

You could sort by case number by doing the sorting within the SOQL query. If you're looking for a more dynamic sort function.

You could also check out: http://salesforcesource.blogspot.com/2008/11/adding-sorting-capability-to.html

and

http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/

More specifically, take a look at your debug log (I use the developer console) what variable is the null pointer exception on? It could be your SortField or PreviousSortField...

or for that matter it could be the cases being passed to super sort


Attribution to: Salesforce Wizard

Possible Suggestion/Solution #2

Looks like either sortField or previousSortField might be null in your doSort method. I'd add a nullcheck on the first if statement there.

Also, an alternative to implementing your own sort method is to write a comparable interface. The comparable interface is a pretty recent enhancement (Spring '12 if I recall), and should suit you better. Check out: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_comparable.htm


Attribution to: James Loghry
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/844

My Block Status

My Block Content