2

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!

1
  • were you able to sort even currency fields using below methods?
    – sfdc
    Jan 24, 2014 at 21:14

2 Answers 2

3

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

1
  • I would like to do this sort upon click.
    – Di Zou
    Aug 23, 2012 at 15:13
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.