0

For the life of me I cannot figure out how to query tasks greater than a given DateTime. I have a field, inqDateTime, that I need to pull tasks greater than that date. I have tried to convert the DateTime to a Date field, but then I get zero returns because it lacks the T and Z from the value, I guess?

So then I tried to use the DAY_ONLY bit in the WHERE clause, but I get errors like crazy expecting a ; .

How do I use a DateTime field and get all tasks from that date?


For reference, here is my code:

@AuraEnabled
public static integer taskQuery(DateTime inqDateTime, id leadID){
    System.debug('militaryQuarantineController.taskCount running.............................................................');

    //Date inqDate = Date.newInstance(inqDateTime.year(), inqDateTime.month(), inqDateTime.day());
    DATE inqDate = inqDateTime.DATE();
    //String inqDate = DateTime
    System.debug('inqDate = ' +inqDate);
    List<Task> taskList = [SELECT id FROM Task WHERE whoid= :leadID AND TaskSubtype!='Task' AND ActivityDate >= : inqDate];
    System.debug('There are ' +taskList.size()+ ' tasks since the last inquiry');

    integer taskCount = taskList.size();

    return taskCount;
}
15
  • can you try this ? System.debug([SELECT id FROM Task WHERE whoid= :leadID AND TaskSubtype!='Task' AND ActivityDate >= : inqDateTime]); Mar 11, 2019 at 15:54
  • That will fail with a bind type error.
    – Phil W
    Mar 11, 2019 at 15:55
  • It is worth noting that when I run a query through workbench I get the values I expect. SELECT ActivityDate,Id,TaskSubtype FROM Task WHERE ActivityDate >= 2018-11-02 AND TaskSubtype != 'Task'
    – Chance
    Mar 11, 2019 at 15:55
  • 2
    @Chance are you executing the queries with the same user in workbench and in your Apex? Could sharing rules be coming in to play?
    – Phil W
    Mar 11, 2019 at 16:02
  • 2
    @Chance do you have the same query running in workbench? There's no issue in the syntax of using the date field here. It could be possibly the other parameters in your where clause.
    – Jayant Das
    Mar 11, 2019 at 16:02

1 Answer 1

3

It seems the problem was elsewhere in my query. I was not passing the leadID correctly after a couple of other edits. Check all your filters, kids.

For future reference, this will work:

DATE inqDate = inqDateTime.DATE();
List<Task> taskList = [SELECT id FROM Task WHERE whoid= :leadID AND TaskSubtype!='Task' AND ActivityDate >= : inqDate];

You must log in to answer this question.

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