0

Event e = new Event();
e.ActivityDate = Date.newInstance(2005,05,25);
insert e; //inserting an event record with all the required fields.

//The below 4 lines are just to check if am doing it as expected.
Date d1 = Date.newInstance(2005,05,05); //Same date in query.
Date d2 = Date.newInstance(2005,06,05); //Same date in query.
Boolean b1 = e.ActivityDate > d1; //This is true
Boolean b2 = e.ActivityDate < d2; //This is true

List<Event> myList = [SELECT id, ActivityDate FROM Event WHERE ActivityDate > 2005-05-05 AND ActivityDate < 2005-06-05];
System.assertEquals(1,myList.size()); //System.AssertException: Assertion Failed: Expected: 1, Actual: 0

The main goal is to filter a few records based on the dates but it won't return any records. The ActivityDate < 2005-06-05 is working fine in the query but not the first part.

//good
List<Event> myList = [SELECT id, ActivityDate FROM Event WHERE ActivityDate < 2005-06-05];
System.assertEquals(1,myList.size()); //true

//bad
List<Event> myList = [SELECT id, ActivityDate FROM Event WHERE ActivityDate > 2005-05-05];
System.assertEquals(1,myList.size()); //System.AssertException: Assertion Failed: Expected: 1, Actual: 0

I should be missing something very silly. Please advice, thank you.

9
  • Silly question but are you use ActivityDate is a field of type Date and not DateTime? Commented Apr 22, 2019 at 22:36
  • Did you expect this to be greater than or equal to? WHERE ActivityDate > 2005-05-05
    – Mark Pond
    Commented Apr 22, 2019 at 23:12
  • @Apz I am not sure what exact data you are using but following worked for me in my Sandbox. Event e = new Event(); e.ActivityDateTime = DateTime.newInstance(2005,05,25,12,00,00); e.ActivityDate = Date.newInstance(2005,05,25); e.DurationInMinutes = 30; insert e; List<Event> myList = [SELECT id, ActivityDate FROM Event WHERE ActivityDate > 2005-05-05 AND ActivityDate < 2005-06-05]; System.assertEquals(1,myList.size()); Commented Apr 23, 2019 at 1:49
  • It is expected to return the list size 0 as ActivityDate is not greater than 2005-05-05 . You have to use DateTime if you want it to work as your expected Commented Apr 23, 2019 at 2:33
  • @CharisMoutafidis ActivityDate is a field of type Date. If this was the case then it'd throw a compile error.
    – apz
    Commented Apr 23, 2019 at 14:50

1 Answer 1

0

@Apz I am not sure what exact data you are using but following worked for me in my Sandbox.

Event e = new Event();
e.ActivityDateTime = DateTime.newInstance(2005,05,25,12,00,00);
e.ActivityDate = Date.newInstance(2005,05,25);
e.DurationInMinutes = 30;
insert e;


List<Event> myList = [SELECT id, ActivityDate FROM Event WHERE ActivityDate > 2005-05-05 AND ActivityDate < 2005-06-05];
System.assertEquals(1,myList.size());
2
  • Great catch, now it returns that event. Although StartDateTime and EndDateTime fields were required on my org. So does this say that ActivityDateTime should be filled, in order to give an ActivityDate?! Also, in my code snippet, both the boolean flags were true but the SOQL query was still returning nothing. Just curious what's happening.
    – apz
    Commented Apr 23, 2019 at 15:02
  • @Apz Dealing with Activities / Events / Tasks can be tricky sometimes. I would suggest try and provide as much information possible as yours had limited in the question. Nonetheless, this community is very active and helpful. All the best. Commented Apr 23, 2019 at 16:45

You must log in to answer this question.

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