2

I use an apex method that takes as inputs 2 dates. And i query on events and then i return the list of events. My code is this:

@AuraEnabled
public static List<Event> getEventsForDateRange(String fromDate, String endDate){
    try{
        date date1 = date.valueOf(fromDate);
        date date2 = date.valueOf(endDate);
        List<Event> eventsList = [select id, Subject, OwnerId, StartDateTime, EndDateTime, Street__c, City__c, Country__c, Postal_Code__c from Event
        where ownerId =: UserInfo.getUserId() AND StartDateTime >: date1 AND StartDateTime <=: date2];
        return eventsList;
    } catch(Exception e) {
        throw new AuraHandledException('Error! ' +e.getMessage());
    }
}

But i give as inputs:fromDate 2020-04-08 and endDate 2020-04-09. And despite the fact that i have events in 09/04/2020 and the owner is my user, the method returns an empty list... Could anyone explain me what i am doing wrong? This is the system.debug that i run in my anonymous console

system.debug(fullCalendarController.getEventsForDateRange('2020-04-08','2020-04-09'));
  • 2
    It could be a case that date is not properly parsed due to locale mismatch. Could you please try if following works: Date.newInstance(2020,04,08)? On another end, if you run same query at Dev console, does it return data? – kurunve Apr 9 at 13:43
  • Hi kurunve and thanks for your response. It does not work if i use Date.newInstance. Yes, if for example i change end date to 2020-04-10, it does return the 2 events that i have. But shouldn'it return when i also use 2020-04-09, since i use " <= " ?? – Giorgos Galaios Apr 9 at 14:08
  • Can you pass date in string and convert back to Date in Apex, I guess Aura methods doesn't support Date type yet. – itzmukeshy7 Apr 9 at 14:10
  • @itzmukeshy7 thanks for the response. Nope, unfortunately this does not work as well. – Giorgos Galaios Apr 9 at 14:15
  • Can you add the update in the question what you are doing. – itzmukeshy7 Apr 9 at 14:16
1

So after a lot of debugging i found solution! Here is my code that works as expected

@AuraEnabled
public static List<Event> getEventsForDateRange(Date fromDate, Date endDate){
    try{
        Time myTime = Time.newInstance(0, 0, 0, 0);
        Datetime fromDt = DateTime.newInstance(fromDate.addDays(1), myTime);
        Datetime toDt = DateTime.newInstance(endDate.addDays(1), myTime);
        system.debug('we have from date: ' +fromDt);
        system.debug('we have to date: ' +toDt);
        List<Event> eventsList = [select id, Subject, OwnerId, StartDateTime, EndDateTime, Street__c, City__c, Country__c, Postal_Code__c from Event
        where ownerId =: UserInfo.getUserId() AND StartDateTime >: fromDt AND StartDateTime <=: toDt];
        for (Integer i=0; i<eventsList.size(); i++) {
            system.debug(eventsList[i].Subject);
            system.debug(eventsList[i].startDateTime);
        }
        system.debug('The query is: select id, Subject, OwnerId, StartDateTime, EndDateTime, Street__c, City__c, Country__c, Postal_Code__c from Event where StartDateTime > ' +fromDt+ ' AND StartDateTime <= ' +toDt);
        return eventsList;
    } catch(Exception e) {
        throw new AuraHandledException('Error! ' +e.getMessage());
    }
}
| improve this answer | |
0

You have to pass Date in String format and in the Apex convert it back to Date for Date and Integer Apex returns null.

@AuraEnabled
public static List<Event> getEventsForDateRange(String fromDate, String endDate){
  Date startDateValue = Date.valueOf(fromDate);
  Date endDateValue = Date.valueOf(endDate);
  try{
    List<Event> eventsList = [select id, Subject, OwnerId, StartDateTime, EndDateTime, Street__c, City__c, Country__c, Postal_Code__c from Event
     where ownerId =: UserInfo.getUserId() AND StartDateTime >: startDateValue AND StartDateTime <=: endDateValue];
    return eventsList;
    } catch(Exception e) {
    throw new AuraHandledException('Error! ' +e.getMessage());
  }
}
| improve this answer | |
  • Still doesn't work. Although i do it by passing string values, it does not change the result. – Giorgos Galaios Apr 9 at 15:11
  • Does it works with Date parameters after removing the ownerId =: UserInfo.getUserId() condition? – itzmukeshy7 Apr 10 at 2:19

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.