I have created a costum object, email. In this object I am storing date and sender. Examples:

Date               sender
8/2/2013 4:00 AM   Amit
8/3/2013 4:00 AM   Jayant
8/3/2013 4:10 AM   Jony

I want to display the data in Visualforce page like this:

Date   8/3/2013
sender : Jony , Jayant //Here i want to display Jony 1st bcz same date but latest time.
Date   8/2/2013
sender : Amit

How can I write a SOQL query for this?


Assuming that your sobject is defined as Email__c and that the fields are Date__c and Sender__c.

The query would be:

SELECT Date__c, Sender__c FROM Email__c ORDER BY Date__c DESC

You can then use visualforce to format your datetime value accordingly:

<apex:outputText value="{0,date,dd.MM.yyyy}">
    <apex:param value="{!object.dateValue}" /> 

More info on visualforce outputText component can be found in the documentation.

EDIT: My mistake, I didn't realise it was really a question about aggregating values.

As user e-bacho 2.0 pointed out in the comments below, the best way to solve this is with a map:

Map<Date, List <Sender__c>>
  • This won't work bcz i want to display date plus sender wise. – Jayant Jadhav Aug 7 '13 at 5:38
  • This soql will give me email object. if i used that object in repeat component i am getting output but how i can print date only once for same date sender then dotted line next again date and sender.. – Jayant Jadhav Aug 7 '13 at 5:43
  • Date 8/3/2013 sender : Jony , Jayant – Jayant Jadhav Aug 7 '13 at 5:46
  • Date 8/2/2013 sender Amit – Jayant Jadhav Aug 7 '13 at 5:49
  • 1
    You will need a Map <Date, List <Sender__c>> in order to map multiple senders from the query for the same date before displaying them on the VF page as you desire. Have a look here - blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections – Boris Bachovski Aug 7 '13 at 6:13

I believe that Sender__c is a lookup to another object. That said here is an example code (not tested, there might be some syntax issues maybe):


// In your class
Map <Date, List <Sender__c>> dateMap {get; set;}
List <Email__c> emails {get; set;}

// Inside your constructor or action method
dateMap = new Map <Date, List <Sender__c>> ();
List <Email__c> emails = [SELECT Date__c, Sender__c, Sender__r.Name FROM Email__c ORDER BY Date__c DESC];

for (Email__c email : emails)
    if (dateMap.get(email.Date__c) == null)
        dateMap.put(email.Date__c, new List <Sender__c> ());

    dateMap.get(email.Date__c).add(new Sender__c(Id = email.Sender__c, Name = email.Sender__r.Name));


<apex:repeat value="{!emails}" var="email">
    <apex:outputText value="{0,date,dd.MM.yyyy}">
        <apex:param value="{!email.Date__c}" /> 

    <apex:repeat value="{!dateMap[email.Date__c]}" var="sender">
        <apex:outputText value="{!sender.Name}" /><br/>


I hope that's what you're after.

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.