{"record":{"id":"63bc6cdfccea22ee","repo":"flowable/flowable-engine","slug":"you-should-call-any-of-the-orderby-methods-first-b-63bc6c","errorCode":null,"errorMessage":"You should call any of the orderBy methods first before specifying a direction","messagePattern":"You should call any of the orderBy methods first before specifying a direction","errorType":"validation","errorClass":"ActivitiIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/AbstractQuery.java","lineNumber":111,"sourceCode":"        orderBy(property);\n        this.nullHandlingOnOrder = nullHandlingOnOrder;\n        return (T) this;\n    }\n\n    @Override\n    public T asc() {\n        return direction(Direction.ASCENDING);\n    }\n\n    @Override\n    public T desc() {\n        return direction(Direction.DESCENDING);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    public T direction(Direction direction) {\n        if (orderProperty == null) {\n            throw new ActivitiIllegalArgumentException(\"You should call any of the orderBy methods first before specifying a direction\");\n        }\n        addOrder(orderProperty.getName(), direction.getName(), nullHandlingOnOrder);\n        orderProperty = null;\n        nullHandlingOnOrder = null;\n        return (T) this;\n    }\n\n    protected void checkQueryOk() {\n        if (orderProperty != null) {\n            throw new ActivitiIllegalArgumentException(\"Invalid query: call asc() or desc() after using orderByXX()\");\n        }\n    }\n\n    @Override\n    @SuppressWarnings(\"unchecked\")\n    public U singleResult() {\n        this.resultType = ResultType.SINGLE_RESULT;\n        if (commandExecutor != null) {","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/AbstractQuery.java#L93-L129","documentation":"AbstractQuery.direction(Direction) applies asc/desc to the property most recently selected via an orderByXX() method. If direction() is called (directly or through asc()/desc()) before any orderBy call, orderProperty is null and ActivitiIllegalArgumentException is thrown. The query API requires sort property first, then direction.","triggerScenarios":"query.desc() / query.asc() / query.direction(Direction.DESCENDING) without a preceding orderByTaskCreateTime(), orderByProcessInstanceId(), etc.","commonSituations":"Dynamic query building where the orderBy call is behind a conditional branch that did not execute; copying API usage from other query frameworks that put direction first; refactors removing the orderBy line while keeping asc()/desc().","solutions":["Call an orderByXX() method before asc()/desc(), e.g. query.orderByTaskCreateTime().desc().","If ordering is optional, guard: only call asc()/desc() when a sort property was chosen.","Never call direction() directly unless you know orderProperty was set by a preceding orderBy call."],"exampleFix":"// before\nTaskQuery q = taskService.createTaskQuery().taskAssignee(user).desc(); // throws\n// after\nTaskQuery q = taskService.createTaskQuery().taskAssignee(user).orderByTaskCreateTime().desc();","handlingStrategy":"validation","validationCode":"if (sortProperty != null) { query = query.orderBy(sortProperty); if (ascending) query = query.asc(); else query = query.desc(); }","typeGuard":null,"tryCatchPattern":"try { return query.asc().list(); } catch (ActivitiIllegalArgumentException e) { logger.warn(\"asc()/desc() without orderBy\"); return query.list(); }","preventionTips":["Always chain orderByXX() immediately before asc()/desc()","In dynamic builders, keep sort property and direction together in one branch","Code-review query chains for the orderBy-then-direction pattern"],"tags":["query","sorting","api-usage-order","illegal-argument"],"backgroundTag":"invalid-api-call-order","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-18T11:17:12.947Z"}