flowable/flowable-engine · error · ActivitiIllegalArgumentException

Invalid query: call asc() or desc() after using orderByXX()

Error message

Invalid query: call asc() or desc() after using orderByXX()

What it means

AbstractQuery.checkQueryOk() is invoked before executing a query (e.g. singleResult(), list()). It throws ActivitiIllegalArgumentException if orderProperty != null, meaning the caller used orderByXX() but never followed it with asc() or desc(). A sort property without a direction leaves the order-by clause incomplete.

Solutions

  1. Terminate every orderByXX() chain with .asc() or .desc().
  2. If direction is dynamic, ensure both branches call it: q.orderByX().(dir ? asc() : desc()).
  3. Only skip ordering entirely — do not call orderBy at all — if no sort is desired.

Example fix

// before
List<Task> tasks = taskService.createTaskQuery().orderByTaskPriority().list(); // throws
// after
List<Task> tasks = taskService.createTaskQuery().orderByTaskPriority().desc().list();
Defensive patterns

Strategy: validation

Validate before calling

// rule: every orderBy must be followed by asc/desc before list()
query = sortCol != null ? query.orderBy(sortCol).(asc ? query.asc() : query.desc()) : query;

Try / catch

try { return query.list(); } catch (ActivitiIllegalArgumentException e) { if (e.getMessage().contains("asc() or desc()")) { return query.asc().list(); } throw e; }

Prevention

When it happens

Trigger: taskQuery.orderByTaskCreateTime().list() — orderBy called but asc()/desc() omitted before list()/singleResult()/count().

Common situations: Refactors dropping the .asc()/.desc() suffix; conditional building where the direction branch was skipped; developers assuming a default direction exists (it does not).

Related errors


AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11). Data as JSON: /api/errors/5fa01942cce80521. Report an issue: GitHub.

Appendix: source

Thrown at modules/flowable5-engine/src/main/java/org/activiti/engine/impl/AbstractQuery.java:121

    @Override
    public T desc() {
        return direction(Direction.DESCENDING);
    }

    @SuppressWarnings("unchecked")
    public T direction(Direction direction) {
        if (orderProperty == null) {
            throw new ActivitiIllegalArgumentException("You should call any of the orderBy methods first before specifying a direction");
        }
        addOrder(orderProperty.getName(), direction.getName(), nullHandlingOnOrder);
        orderProperty = null;
        nullHandlingOnOrder = null;
        return (T) this;
    }

    protected void checkQueryOk() {
        if (orderProperty != null) {
            throw new ActivitiIllegalArgumentException("Invalid query: call asc() or desc() after using orderByXX()");
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public U singleResult() {
        this.resultType = ResultType.SINGLE_RESULT;
        if (commandExecutor != null) {
            return (U) commandExecutor.execute(this);
        }
        return executeSingleResult(Context.getCommandContext());
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<U> list() {
        this.resultType = ResultType.LIST;
        if (commandExecutor != null) {

View on GitHub (pinned to d6d39ce1c6)