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
Veronica CoiinaVeronica Coiina 

what can i do for order by birthdate asc a table

i need create an extension controller and a vp where i can in a pdf document a table with name, birthdate,department, etc.  this is contact info of an account and i need order this table for birthdate. 

my vp:
<apex:page applyHTMLTag="true" applyBodyTag="false" showHeader="false" renderas="pdf" standardController="Account" extensions="ContactsListController" >
<style type="text/css">
<apex:stylesheet value="{!URLFOR($Resource.style)}"/>
   </style>
<html>
   <body>
        <div style="float: left; ">
         <apex:image alt="cargill" title="cargill"
              url="{!URLFOR($Resource.cargill)}"/>    
     </div> 
        <div style="float: right; ">
    <br/>     
   <apex:outputText value="Caracas,{0,date, dd/MM/yyy}">
       <apex:param value="{!NOW()}" />
                   </apex:outputText><br/>
          {! IF($User.isActive, $User.FirstName & ' ' & $User.LastName,  'inactive') } <br/>
           {! Account.Name }
      
    </div> 
          <div style="clear:both;"> </div><br/><br/><br/>
           <apex:pageBlock >
               
    <apex:pageBlockTable value="{!Account.contacts}" var="contact">
         
        <apex:column value="{!contact.Name}"/>
        <apex:column value="{!contact.Department}"/>
        <apex:column value="{!contact.Phone}"/>
        <apex:column value="{!contact.Gender__c}"/> 
        <apex:column value="{!contact.Birthdate}"/>    
        <apex:column >
        <apex:facet name="header">Age</apex:facet>
                        {!year(today())-year(contact.birthdate)- 1}
        </apex:column> 
   
    </apex:pageBlockTable>
          </apex:pageBlock>
     
     
   </body>
</html>       
</apex:page>

my class:

public class ContactsListController {
private final Account acct;
    private String sortOrder = 'birthdate';
 public ContactsListController(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }   
    
     
public List<Contact> getContacts() {
   
       List<Contact> results = Database.query(
        'SELECT name,Department, Phone, Gender__c, Birthdate ' + 'FROM account' +
        
        'ORDER BY ' + sortOrder + ' desc '
    );
    return results;
}

}
Veronica CoiinaVeronica Coiina
thanks Amit,
But i need table only show contacts associated with the account, not all contacts.
you know how i can do it?
 
Amit Chaudhary 8Amit Chaudhary 8
In your case you can change code like below :-

Option 1:-
public class ContactsListController 
{
	private final Account acct;
    private String sortOrder = 'birthdate';
	public ContactsListController(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }       
	public List<Contact> getContacts() 
	{
		List<Contact> results = [SELECT name,Department, Phone, Gender__c, Birthdate FROM contact where accountId != null ORDER BY Birthdate desc ];
		return results;
	}
}

Option 2 :- New Page for you :- Account and all record Contact Detail
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
    <apex:pageBlock >
          <apex:repeat value="{!accounts}" var="a">
<apex:pageBlockSection title="{!a.name}"></apex:pageBlockSection>
  <apex:relatedList list="Contacts" subject="{!a.Id}"/>
<apex:relatedList list="Opportunities" subject="{!a.Id}" />
</apex:repeat>      
     </apex:pageBlock>
</apex:page>
Let us know if this will help you

 
Veronica CoiinaVeronica Coiina
thanks, but i need show in my pdf only contacts associated with an account, not all accounts with your contacts. if you can help me please!
Amit Chaudhary 8Amit Chaudhary 8
I hope your Query is resloved in below post
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000D9NY
Please update your class like below :-
 
public class ContactsListController 
{   
   	public Account acct;
	Public List<Contact> lstCont {get;set;}
   	public ContactsListController(ApexPages.StandardController stdController) 
	{
        this.acct = (Account)stdController.getRecord();
		lstCont = new List<Contact>();
		
		if(acct.id != null)
		{
			lstCont = [ SELECT Name, department,phone,gender__c, birthdate 
										FROM contact 
										WHERE accountid =:acct.id
										ORDER BY birthdate desc
								] ;
		}
    }       
}
VF page like below :-
<apex:page applyHTMLTag="true" applyBodyTag="false" showHeader="false" renderas="pdf" standardController="Account" extensions="ContactsListController" >
<style type="text/css">
<apex:stylesheet value="{!URLFOR($Resource.style)}"/>
   </style>
<html>
   <body>
        <div style="float: left; ">
         <apex:image alt="cargill" title="cargill"
              url="{!URLFOR($Resource.cargill)}"/>    
     </div> 
        <div style="float: right; ">
    <br/>     
   <apex:outputText value="Caracas,{0,date, dd/MM/yyy}">
       <apex:param value="{!NOW()}" />
                   </apex:outputText><br/>
          {! IF($User.isActive, $User.FirstName & ' ' & $User.LastName,  'inactive') } <br/>
           {! Account.Name }
      
    </div> 
      	<div style="clear:both;"> </div><br/><br/><br/>
           <apex:pageBlock >
               
    <apex:pageBlockTable value="{!lstCont}" var="contact">
	    <apex:column value="{!contact.Name}"/>
        <apex:column value="{!contact.Department}"/>
        <apex:column value="{!contact.Phone}"/>
        <apex:column value="{!contact.Gender__c}"/> 
        <apex:column value="{!contact.Birthdate}"/>    
        <apex:column >
        <apex:facet name="header">Age</apex:facet>
                        {!year(today())-year(contact.birthdate)- 1}
        </apex:column> 
   
    </apex:pageBlockTable>
          </apex:pageBlock>
     
     
   </body>
</html>       
</apex:page>
Please let us know if this will help you