Date range query for HistoricTaskInstanceQuery does not work

hello there,

I tried using taskCreatedAfter and taskCreatedBefore to set date range for querying historic task list, and used taskWithoutDeleteReason to remove the deleted task. A simple example as:

    // using Flowable 7.0.1 in java 21
    public List<HistoricTaskInstance> finishedList() {
        HistoricTaskInstanceQuery taskInstanceQuery = historyService.createHistoricTaskInstanceQuery()
                .taskWithoutDeleteReason()
                .orderByHistoricTaskInstanceEndTime().desc();

        // to find results started between period, not ok
        taskInstanceQuery.taskCreatedAfter(DateUtils.parseDate("2025-11-28"));
                         .taskCreatedBefore(DateUtils.parseDate("2025-12-01"));

        // to find results completed between period, not ok
        // taskInstanceQuery.taskCompletedAfter(DateUtils.parseDate("2025-11-28"));
        //                  .taskCompletedBefore(DateUtils.parseDate("2025-12-01"));

        return taskInstanceQuery.list();
    }

However, it didn’t return tasks created (started) between 2025-11-28 and 2025-12-01 as expected; instead, it just returned all tasks (included the deleted tasks, so taskWithoutDeleteReason also didn’t work). taskCompletedAfter and taskCompletedBeforehas the same problem.

Then I tried setting one of taskCompletedAfter, taskCompletedBefore, taskCreatedAfter, or taskCreatedBefore individually, and they all worked as expected on their own. However, combining them produced results that i cant understand.

But for the similar logic (i.e. find the object started or completed during target time range), I can set date range condition successfully on historyService.createHistoricProcessInstanceQuery() and taskService.createTaskQuery(), for example:

// it works as expected
HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery();
historicProcessInstanceQuery.startedAfter(query.getBeginStartTime());
                            .startedBefore(query.getEndStartTime());

// it works as expected
TaskQuery taskQuery = taskService.createTaskQuery();
taskQuery.taskCreatedAfter(DateUtils.parseDate("2025-11-28"));
         .taskCreatedBefore(DateUtils.parseDate("2025-12-01"));

Why is this happening? Did I go wrong?

Thanks in advance. :smiling_face_with_three_hearts:

Hey @kM-Stone,

I am a bit confused. You are using taskCreatedAfter and taskCreatedBefore and you expect the query to return tasks completed between a certain period. The query options you are using will return you task that have been created in the requested period.

What you need to use is taskCompletedAfter and taskCompletedBefore, this will return you the tasks that have been completed in a given period.

I’m not fully following this. What do you mean by it? In that example you are looking for all process instances started after a certain date.

Cheers,
Filip

Thanks for your reply, Filip!
I’m sorry to my wrong description and I edited my question.

Actually, we want to get the tasks created in a given period, instead of completed tasks. And we also tried taskCompletedAfter and taskCompletedBefore as you advice, but they also return unexpected results (return all tasks instead of the tasks subject to my condition).
I tried setting one of taskCompletedAfter, taskCompletedBefore, taskCreatedAfter, taskCreatedBefore individually, and they all worked as expected on their own. However, combining them produced results that i cant understand.

sorry, I copied a wrong code and it has been updated. What I’m trying to say here is that similar query logic works in other queryObjects.

Thanks for clarifying it @kM-Stone.

I had a better look and we had a bug in 7.0.1 there. We were using or instead of and for taskCreatedAfter and etc.

This has been fixed in 7.1.0 and later. Can you try upgrading and see if it works for you? The latest version is 7.2.0.

Cheers,
Filip

Yes, upgrading to 7.2.0 works for me. Thank you !! :clap: