{"record":{"id":"871eebe6c563b8ed","repo":"flowable/flowable-engine","slug":"the-query-is-already-in-an-or-statement-871eeb","errorCode":null,"errorMessage":"the query is already in an or statement","messagePattern":"the query is already in an or statement","errorType":"validation","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-task-service/src/main/java/org/flowable/task/service/impl/HistoricTaskInstanceQueryImpl.java","lineNumber":2066,"sourceCode":"        return this;\n    }\n\n    @Override\n    public HistoricTaskInstanceQuery includeCaseVariables() {\n        this.includeCaseVariables = true;\n        return this;\n    }\n\n    @Override\n    public HistoricTaskInstanceQuery includeIdentityLinks() {\n        this.includeIdentityLinks = true;\n        return this;\n    }\n\n    @Override\n    public HistoricTaskInstanceQuery or() {\n        if (inOrStatement) {\n            throw new FlowableException(\"the query is already in an or statement\");\n        }\n\n        inOrStatement = true;\n        if (databaseType != null) {\n            currentOrQueryObject = new HistoricTaskInstanceQueryImpl(commandExecutor, databaseType, taskServiceConfiguration, variableServiceConfiguration);\n        } else {\n            currentOrQueryObject = new HistoricTaskInstanceQueryImpl(commandExecutor, taskServiceConfiguration, variableServiceConfiguration);\n        }\n        orQueryObjects.add(currentOrQueryObject);\n        return this;\n    }\n\n    @Override\n    public HistoricTaskInstanceQuery endOr() {\n        if (!inOrStatement) {\n            throw new FlowableException(\"endOr() can only be called after calling or()\");\n        }\n","sourceCodeStart":2048,"sourceCodeEnd":2084,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-task-service/src/main/java/org/flowable/task/service/impl/HistoricTaskInstanceQueryImpl.java#L2048-L2084","documentation":"Flowable throws this FlowableException when HistoricTaskInstanceQuery.or() is invoked while the query is already inside an active or-block. Nested or-statements are not supported by the query model, so a second or() before endOr() is rejected. It is a query-usage (state) error raised at build time.","triggerScenarios":"Calling query.or() ... query.or() without an intervening endOr(), e.g. chaining two or-groups or accidentally calling or() inside an already open or-block.","commonSituations":"Building queries programmatically where or()/endOr() are called in loops or helper methods that each open their own or-block; copy-pasted query code where an endOr() was deleted.","solutions":["Call endOr() to close the current or-block before starting a new one with or().","Restructure the query so each or-group is opened and closed exactly once (or() ... endOr()).","Combine the conditions of the second group into the first or-block if they belong together.","Track the inOr state in your builder helper to avoid calling or() twice."],"exampleFix":"// before\nquery.or().taskName(\"a\").or().taskName(\"b\").endOr(); // throws\n// after\nquery.or().taskName(\"a\").taskName(\"b\").endOr();","handlingStrategy":"validation","validationCode":"boolean inOr = ((HistoricTaskInstanceQueryImpl) query).isInOrStatement(); // guard before or()\nif (!inOr) { query.or(); }","typeGuard":null,"tryCatchPattern":"try {\n    query.or();\n} catch (FlowableException e) {\n    if (e.getMessage().contains(\"already in an or statement\")) {\n        throw new IllegalStateException(\"Query builder misuse: or() called twice before endOr()\", e);\n    }\n    throw e;\n}","preventionTips":["Always pair or() and endOr() in the same method or block","Never call or() inside a helper that may itself be invoked within an or-block","Structure query building linearly instead of conditionally opening or-groups","Write a unit test for each dynamic query path to catch unbalanced or() early"],"tags":["flowable","query-builder","or-statement","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}