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
ppiyushppiyush 

sorting pageBlockTable

Hi Everyone,

 

I have seen this wiki article http://wiki.developerforce.com/index.php/Sorting_Tables. I implemented this exactly in the sandbox and the code works absolutely fine.

 

Now, I have tried to adapt this to a different apex class / visualforce page, but somehow the sorting doesnt work when I put the doSort function into an extesion class. see below:

 

Visualforce Page

 

<apex:page standardController="Account" extensions="testContact">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!cntz}" var="o" id="table">
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Contact.Fields.Name.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                        </apex:commandLink> 
                    </apex:facet>
                    <apex:inputField value="{!o.name}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

And here is the apex class which is the extension to the Accoun standardController in the visualforce page above:

 

public class testContact {
    
    private List<Contact> cntz;
    private Account acct; 
    public String sortField {get; set;}
    public String previousSortField {get; set;}

    public testContact(ApexPages.StandardController controller) {
        this.acct= (Account)controller.getRecord();
    }
    
    public Account getAt()
    {
        Account[] at = [Select id,name FROM Account where id= :acct.id];
        if(at.size()>0){
    		return at[0];
    	}
    	else{
    		return null;
    	}
    }
            
    public List<Contact> getCntz()
    {
        Account act = [Select id FROM Account where id = :acct.id];
        cntz = [Select id, Name, Client_Potential__c, title, Address_City__c, Direct_Phone__c, Email, Days_Since_Last_Contact__c, Owner.alias, Divisional_Account__c from Contact where ((account.parentid = :act.id OR account.id = :act.id)AND LastName!='Budget')];
    	return cntz;
    }

    public void UpdateRecords() {
        // this simple line of code finds out which column was changed and update the 
        // relevant account record accordingly!
        update cntz;
    }
    
        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(cntz,sortField,order);
    }


}

 

 The main purpose of this class is to show all children contacts of a parent account (even if they are attached to a child account of a prent account). Now once I have this list, I would like to sort it (which is why I used the sorting funciton), but somehow this doesnt work. No errors in testing, but just doesnt work as expected!

 

Any help would be much appreciated.

 

Thanks,

Pranav

bob_buzzardbob_buzzard

I think this is because your getCntz method is always retrieving the list of contacts anew, and thus they will appear in the order they are retrieved from the database.

 

If you change this method to only retrieve the data from the database if the list is null (which it will be first time through only), then you will return the sorted values.

ppiyushppiyush

Bob,

 

you were absolutely right, and that seems to have solved the sortng problem. However, now I was expanding my page, and came across another problem.

 

I have a lookup field on contact called Divisional Account (looking up accounts). I want to be able to sort based on this, but when I try that I get an error  - Developer Script Exception - superSort : Attempt to de-reference a null object

 

The code where this error is created in superSort is an if statement that tries tofigure out the type of the sortField:

 

        /*Determine the type of the field that needs to be sorted, if it is a 
        reference we will want sort by the name of the related object, not the 
        ID itself*/
        if(items[0].getSObjectType().getDescribe().fields.getMap().get(sortField).getDescribe().getType().Name() == 'REFERENCE'){
            isSortFieldReference = true;
            referenceName = new Map<Id,String>();
            
            /*Determine the type of this object and populate the Id to Name map*/
            Set<Id> referenceIds = new Set<Id>();
            for(sObject s : items){
               referenceIds.add((Id)s.get(sortField));
            }

            String objectID = (String)items[0].get(sortField);
            String prefix = objectID.substring(0,3);
            String objectType;
            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
            for(Schema.SObjectType s : gd.values()){
                if(prefix == s.getDescribe().getKeyPrefix()){
                    objectType = s.getDescribe().Name;
                }
            }
            
            //Query the related objects for the name and populate the Id -> Name map
            String queryString = 'select Id, Name from ' + objectType + ' where ID IN :referenceIDs';
            for(sObject s : Database.query(queryString )){
                referenceName.put((Id)s.get('Id'),(String)s.get('Name'));
            }
        }

 Now, my VF page looks like:

 

<apex:page standardController="Account" extensions="testContact">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!cntz}" var="o" id="table">
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Contact.Fields.Name.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                        </apex:commandLink> 
                    </apex:facet>
                    <apex:outputField value="{!o.name}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Contact.Fields.Divisional_Account__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Divisional_Account__c" assignTo="{!sortField}"/>
                        </apex:commandLink> 
                    </apex:facet>
                    <apex:outputField value="{!o.Divisional_Account__c}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

Any help will be much appreciated.

bob_buzzardbob_buzzard

This smacks that Divisional_Account__c is not recognised as a field on the contact, but I'd expect there to be errors when saving the page if that were the case.

 

Try adding some debug around this line:

 

 

 if(items[0].getSObjectType().getDescribe().fields.getMap().get(sortField).getDescribe().getType().Name() == 'REFERENCE'){

 

 

e.g.

 

 

System.debug('##### Fields = ' + items[0].getSObjectType().getDescribe().fields.getMap());

 

and try to find where the result of one of the nested method calls returns null.

 

 

deep12usdeep12us

When I was trying to sort the datatable, it sorts fine but the resulting sort order is based on the ASCII values. Is there a way to sort normally