1

I have couple of Question on datetime fields.

Question 1: How to pass current month beginning date time and ending datetime in Apex

Here I setted"July" Month start datetime and end date time values

   Datetime  currentStart=DateTime.newInstance(2018, 7, 1, 0, 0, 0);
   Datetime CurrentEnd= DateTime.newInstance(2018, 7, 31, 0, 0, 0);

If I pass these values in following query:

   Datetime  currentStart=DateTime.newInstance(2018, 7, 1, 0, 0, 0);
   Datetime CurrentEnd= DateTime.newInstance(2018, 7, 31, 0, 0, 0);

  String q = 'SELECT Id,' + 
        String.valueOf(titleField) + ',' + 
        String.valueOf(startDateTimeField) + ',' +
        String.valueOf(endDateTimeField) + ',' +
        String.valueOf(descriptionField) + ',' +
        String.valueOf(whatIdField) + ',' +
        String.valueOf(whoIdField) + ',' +
        String.valueOf(userField) + ' FROM ' + 
        String.valueOf(sObjectName) + ' WHERE ' + 
        String.valueOf(userField) + ' = \'' + UserInfo.getUserId() + '\'' +
        ' And ' + String.valueOf(startDateTimeField) + '=\''+'Currentstart'+'\' And ' +
        String.valueOf(endDateTimeField) + '=\''+'CurrentEnd'+'\'';

Output from above Query:

SELECT Id,Subject,StartDateTime,EndDateTime,Description,WhatId,WhoId,OwnerId 
FROM Event WHERE IsDeleted = FALSE And StartDateTime='2018-05-30 18:30:00' And EndDateTime='2018-07-30 18:30:00'

Error Received:

 FATAL_ERROR System.QueryException: value of filter criterion for field 'StartDateTime' must be of type dateTime and should not be enclosed in quotes

Question 2:How to ressolve above(date time) error

If I run below query for testing Purpose it will work:

I had given this Query for Example:

SELECT Id FROM Contact WHERE CreatedDate >= 2014-07-20T01:02:03Z
0

You can try this. Basically you have to get the current month method tostartofmonth as start and in end append month to 1 and -1 to days . Once after that pls split the days year and month to string and convert that to datetime format.

   Date currentStart = System.Date.today().toStartOfMonth(); 
   Date CurrentEnd = currentStart.addMonths(1).addDays(-1);

   Integer sdy = currentStart.day();
   Integer smon = currentStart.month();
   Integer syr = currentStart.year();
   Integer edy = CurrentEnd.day();
   Integer emon = CurrentEnd.month();
   Integer eyr = CurrentEnd.year();
   string cdate = syr+'-'+'0'+smon+'-'+'0'+sdy+'T00:00:00Z';
   string edate = eyr+'-'+'0'+emon+'-'+edy+'T00:00:00Z';
   String q = 'SELECT Id,' + 
       String.valueOf(titleField) + ',' +
       String.valueOf(startDateTimeField) + ',' +
       String.valueOf(endDateTimeField) + ',' +
       String.valueOf(descriptionField) + ',' +
       String.valueOf(whatIdField) + ',' +
       String.valueOf(whoIdField) + ',' +
       String.valueOf(userField) + ' FROM ' + 
       String.valueOf(sObjectName) + ' WHERE ' + 
       String.valueOf(userField) + ' = \'' + UserInfo.getUserId() + '\'' +
       //' And ' + String.valueOf(startDateTimeField) + '=\''+cdate+'\' And ' +
       //String.valueOf(endDateTimeField) + '=\''+edate+'\'';

       ' And ' + String.valueOf(startDateTimeField) + '>='+cdate+' And ' +
       String.valueOf(endDateTimeField) + '<='+edate;
1

If you read the Error message carefully, for dates you don't need to enclose them in quotes.

String.valueOf(startDateTimeField) + '=\''+'Currentstart'+'\' 

gives you

StartDateTime='2018-05-30 18:30:00'

And what you need to get id StartDateTime=2018-05-30T18:30:00Z

So you can utilize the DateTime.format() method to achieve this.

String.valueOf(startDateTimeField) + '=' + Currentstart.format()

There are several implementations of the format() method, so choose the one that fits your needs.

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.