0

I am trying to display today’s event ina vfpage like Salesforce today event component does, but my query is not resulting in the correct list based on user time zone when putting where clause to today. Rather it is giving the results based on UTC date time stored in the database. Using soql convert timezone method on startdatetime field. like this Day_Only(convertTimezone(StartDateTime))=Today. but I noticed it's not returning the Events according to current user timezone. here is my complete code.

public with sharing class EventExt {

public List<Event> getEvents() {
    List<Event>  eventList = new List<Event>();
    eventList  = [SELECT id, subject,format(StartDateTime) from event  WHERE (ownerId =:UserInfo.getUserID()  and Day_Only(convertTimezone(StartDateTime)) = Today )  limit 10];
    return eventList;        
}  }


<apex:page controller="EventExt" >
<apex:pageBlock >
    <apex:pageBlockTable value="{!Events}" var="event">
        <apex:column value="{!event.subject}"/>
        <apex:column value="{!event.StartDateTime}"/>
    </apex:pageBlockTable>
</apex:pageBlock>

I have attached my visualforce page result and salesforce component display today Events.

enter image description here

1 Answer 1

1

The date literals (TODAY, YESTERDAY, etc) already use the current user's time zone, so there's no need to convert the values. Additionally, Visualforce already converts date and date/time fields to the current user's time zone under normal circumstances. There's no need either way to use the format() or convertTimeZone() functions in your SOQL. Normally, you only need to use convertTimeZone if you're using aggregate functions (e.g. listing all records created during a certain hour, it would show the GMT hour without this function).

Your method should look more like this:

public List<Event> getEvents() {
    return [SELECT subject, StartDateTime 
            from event  
            WHERE ownerId = :UserInfo.getUserID() AND 
                  StartDateTime = Today limit 10];
}
3
  • When I use date literals, it not working correctly with all-day event, for example, i have tomorrow all day event but when i query its return in today event instead of tomorrow. even the select field startdatetime showing its tomorrow event.
    – Faisal
    Commented Oct 29, 2018 at 15:32
  • @Faisal I'm guessing you're setting the wrong time? You definitely do not need to mix convertTimeZone with date literals.
    – sfdcfox
    Commented Oct 29, 2018 at 15:46
  • sure I was using convertTimeZone with date.today which create more problem for this i decided to switch to soql date literals but notice that its return tomorrow all day event to today date
    – Faisal
    Commented Oct 29, 2018 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .