1

I want to display a certain group of Events in Visualforce page - my query to only get Events that were created/modified after the Event happened - first tried with a custom field, but this proved unreliable to manage the field:

public class UsersEventsExt {

User U;

public UsersEventsExt(ApexPages.StandardController controller) {
  U = (User) controller.getRecord();        
}

public List<Event> getEvents() {
  return [select subject, description, activitydate, Whatid, WhoId, comments_summary__c  from Event where Ownerid = :UserInfo.getUserId() and Customfield__c = false];
}

}

I can get the events list by changing query to:

public List<Event> getEvents(datetime ActivityDatetime) {
      return [select id, subject, description, activitydate, Whatid, WhoId, comments_summary__c  from Event where Ownerid = :UserInfo.getUserId() and LastModifiedDate < :ActivityDatetime ];
    }

But then my VF page fails. Here is what worked with the first query class:

<apex:page standardController="User" extensions="UsersEventsExt2">

  <apex:pageBlock title="Meetings">
    <apex:pageBlockTable value="{!Events}" var="c">

      <apex:column headerValue="Click to Update"><apex:outputLink value="{!URLFOR($Action.Event.Edit,c.id)}" target="_blank">Update Meeting</apex:outputLink></apex:column>

       <apex:column headerValue="Subject"><apex:outputLink value="{!URLFOR($Action.Event.View,c.id)}" target="_blank">{!c.subject}</apex:outputLink></apex:column>
      <apex:column headerValue="Name" value="{!c.WhoId}"/>
       <apex:column headerValue="Date" value="{!c.ActivityDate}"/>
       <apex:column headerValue="Account" value="{!c.WhatId}"/>


    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

The error lies in not getting Events from the query in the class I think? Though it looks like it does return them! any help appreciated! Thanks!

  • probably might be an issue with timezone. try to adjust your input date value to match with the timezone of the user. also try to execute this query with the hardcoded date value to find if the query really returns records...hope this hint will help you – Bforce Feb 6 '14 at 16:21
  • like baskaran said, try to run the query in a query editor. Use system.debug messages to find out what date is actually being passed into the getEvents method. – willard Feb 6 '14 at 19:55
  • I am trying this. I am not getting why the ActivityDateTime needs to be set. It's a field on the object. I can run a query like 'select id from Event where ActivityDateTime > 2013-05-08T21:00:00.000Z' with no problem. – jaw999 Feb 6 '14 at 21:36
  • The issue, as others have pointed out, is that when your query is currently running, the activitydatetime you are using for comparison is null, and hence you are seeing all records come back. How are you selecting the activitydatetime to compare against? Are you trying to have a datetimepicker on the Visualforce page and use that for selecting the events, or are you going off of the current time (e.g. Datetime.now()), or something else? – James Loghry Feb 6 '14 at 22:21
3

Having consulted with my colleagues, it appears you are right to ask about this, because comparing DateTimes is not currently supported by SOQL and thus trying to do this in-query is not going to work.

Therefore, I suggest you will need to make the date comparison in code, if you put this method in your controller, it appears to work for me:

public List<Event> getEvents() {
    List<Event> results = new List<Event>();
    List<Event> allEvents = [SELECT id, subject, description, activitydate, Whatid, WhoId, comments_summary__c, ActivityDatetime, LastModifiedDate 
            FROM Event
            WHERE Ownerid = :UserInfo.getUserId()];
    for(Event thisEvent : allEvents)
    {
        if(thisEvent.LastModifiedDate < thisEvent.ActivityDatetime)
        {
            results.add(thisEvent);
        }            
    }
    return results;
}

Of course, this is a slightly heavier operation than just returning the result of the SOQL query, so - depending on the size of your org/implementation, you should keep yourself aware of the governor limits for SOQL query results (50,000 rows) and so on. This is a working solution to list the rows that you are after though.

  • I may need to batch process old records so to limit the results. – jaw999 Feb 7 '14 at 17:01
  • if only i could get the post edit from inline to return to the home url from this link: <apex:outputLink value="{!URLFOR($Action.Event.Edit,c.id)}" – jaw999 Feb 7 '14 at 17:15
1

How are you passing the ActivityDatetime into the Events getter? If this is basically the sum of your code, the autowired access to getEvents from the VF page won't work with a parameter in there.

If you are after all the events that have a LastModifiedDate before their ActivityDateTime, just remove the parameter from your getter like this:

public List<Event> getEvents() {
      return [select id, subject, description, activitydate, Whatid, WhoId, comments_summary__c from Event where Ownerid = :UserInfo.getUserId() and LastModifiedDate < :ActivityDatetime];
}

You wouldn't even be able to save this code/VF in the format you have presented above (without a valid getter for Events, the VF page can't be saved as it will reference a property it cannot access in the standard or extended controller).

  • That query produces errors as described in my initial post: ` Error Error: Compile Error: Variable does not exist: ActivityDatetime at line 11 column 170' – jaw999 Feb 6 '14 at 18:02
  • Apparently the fields need to be passed in to work? i don't get it. – jaw999 Feb 6 '14 at 18:04
  • Please see my new answer on here, hopefully now having worked out the problem! – Simon Lawrence Feb 7 '14 at 10:05
1

You cannot pass the variable into the method through the VF page. Your method must use the

public List getEvents()

version. Make sure you have it instead as a variable in your class somewhere and set it outside of your method call:


public class UsersEventsExt {
  User U;
  public DateTime ActivityDatetime{get; set;}
  • Hmm, I am missing a step - this query below returns all Events regardless of ActivityDateTime – jaw999 Feb 6 '14 at 19:39
  • public class UsersEventsExt2 { User U; public UsersEventsExt2(ApexPages.StandardController controller) { U = (User) controller.getRecord(); } public DateTime ActivityDatetime{get; set;} public List<Event> getEvents() { return [select id, subject, description, activitydate, Whatid, WhoId, comments_summary__c from Event where Ownerid = :UserInfo.getUserId() and LastModifiedDate < :ActivityDatetime ]; } } – jaw999 Feb 6 '14 at 19:53
  • You need to set the variable somewhere. Currently your ActivityDateTime is null. Or are you instead thinking of comparing the ActivityDateTime field within the query like you would with SQL? One thing to keep in mind is the differences between SOQL and SQL. If you wanted it similar to the SQL functionality then you should follow @Simon Lawrences example. – dphil Feb 7 '14 at 16:48

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.