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
harish reddy 50harish reddy 50 

I am implementing sorting in pageblocktable but i am getting (Unknown property 'contactsortclass.contact') below is my code

controller:-
public class contactsortclass {
    
    public List<Contact> contacts;
   public String sortDirection = 'ASC'; 
   public String sortExp = 'name'; 
  
   public String sortExpression 
   { 
     get 
     { 
        return sortExp; 
     } 
     set 

     { 

       if (value == sortExp) 
         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC'; 
       else 
         sortDirection = 'ASC'; 
       sortExp = value; 
     } 
   } 
 public String getSortDirection() 

 { 
    

    if (sortExpression == null || sortExpression == '') 
      return 'ASC'; 
    else 
     return sortDirection; 
 } 
 public void setSortDirection(String value) 

 {   
   sortDirection = value; 

 } 
   public List<contact> getAccounts() { 
       return contacts; 
   } 

   public PageReference ViewData() { 

        
       string sortFullExp = sortExpression  + ' ' + sortDirection; 

         
       contacts = Database.query('Select id,firstname,accountname,department,mobile order by ' + sortFullExp + ' limit 1000'); 
       return null; 
   } 
    
    public  void save(){
    
    update contacts;
    }
}

apex:-


<apex:page controller="contactsortclass" tabStyle="contact"> 
<apex:sectionHeader title="contacts List with Sorting"></apex:sectionHeader> 
<apex:form > 
<apex:pageBlock title="" id="pageBlock"> 
    <apex:pageMessages ></apex:pageMessages> 
    <apex:pageBlockTable value="{!contacts}" var="a" rendered="{!NOT(ISNULL(accounts))}"> 

       <apex:column > 
         <apex:facet name="header">   
           <apex:commandLink action="{!ViewData}" value=" firstname{!IF(sortExpression=='firstname',IF(sortDirection='ASC','▼','▲'),'')}" id="cmdSort"> 
             <apex:param value="firstname" name="column" assignTo="{!sortExpression}" ></apex:param> 

           </apex:commandLink> 
         </apex:facet> 
         <apex:outputLink value="/{!a.Id}" target="_blank">{!a.Name}</apex:outputLink> 
       </apex:column> 
       
        
    </apex:pageBlockTable> 
    <apex:pageBlockButtons location="bottom"> 
      <apex:commandButton value="save" action="{!getaccounts}" id="theButton" rerender="pageBlock"></apex:commandButton> 
      <apex:commandButton value="edit" action="{!ViewData}" id="theButton1" rerender="pageBlock"></apex:commandButton>
        <apex:commandButton value="cancel" action="{!ViewData}" id="theButton2" rerender="pageBlock"></apex:commandButton>
    </apex:pageBlockButtons>
    <apex:panelGrid columns="1" style="float:left">
      <apex:commandButton value="previous" action="{!viewdata}"/>
    </apex:panelGrid>
    <apex:panelGrid columns="1" style="float:right">
      <apex:commandButton value="next" action="{!viewdata}"/>
    </apex:panelGrid>
</apex:pageBlock> 
</apex:form> 
</apex:page>

 
Karan Shekhar KaulKaran Shekhar Kaul
Hi Harish,

Cannot see getter for {!contacts}

Try below controller 

public class contactsortclass {
    
    public List<Contact> contacts{get;set;}
   public String sortDirection = 'ASC'; 
   public String sortExp = 'name'; 
  
   public String sortExpression 
   { 
     get 
     { 
        return sortExp; 
     } 
     set 

     { 

       if (value == sortExp) 
         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC'; 
       else 
         sortDirection = 'ASC'; 
       sortExp = value; 
     } 
   } 
 public String getSortDirection() 

 { 
    

    if (sortExpression == null || sortExpression == '') 
      return 'ASC'; 
    else 
     return sortDirection; 
 } 
 public void setSortDirection(String value) 

 {   
   sortDirection = value; 

 } 
   public List<contact> getAccounts() { 
       return contacts; 
   } 

   public PageReference ViewData() { 

        
       string sortFullExp = sortExpression  + ' ' + sortDirection; 

         
       contacts = Database.query('Select id,firstname,accountname,department,mobile order by ' + sortFullExp + ' limit 1000'); 
       return null; 
   } 
    
    public  void save(){
    
    update contacts;
    }
}
 
GauravGargGauravGarg
Hi Harish,

Please declare Contact like below:
public List<Contact> contacts{get;set;}
no trailing semi-colon (;)

Hope this helps.

Thanks,
Gaurav
skype: gaurav62990