When Should Business Logic Live in Workflow vs Java Code?
Designing a workflow raises a question that gets answered by accident far more often than by decision: where does this particular rule belong? This is not about carrying decisions between stages, which is a separate design concern. It is the narrower and more frequent question of placement - the developer has a rule in front of them, and four plausible homes.
The four homes
| Home | Naturally good for | Bad for | Changed by |
|---|---|---|---|
| The workflow definition | Routing, assignment, stage sequence | Calculation, validation, anything reused elsewhere | A developer, via redeploy |
| The service layer | Domain rules, validation, calculation, integration | Sequencing and human task assignment | A developer, via release |
| A rules engine | Volatile policy with many conditions and variants | Simple or rarely changing rules | An analyst, with governance |
| Declarative model configuration | Field validation, allowed values, permissions | Anything conditional across entities | An administrator, at runtime |
Most teams use two of these by default - the workflow definition and the service layer - and place rules in whichever one they were working in at the time. That is how a calculation ends up inside a transition script and a stage sequence ends up hardcoded in a Java class, both in the same application.
The question that settles most cases
Before the nuanced criteria, one question resolves the majority of placements: does this rule need to apply when the workflow is not involved?
Records get created by batch imports, corrected by administrators, migrated from another system, and modified through API calls. If a rule must hold for all of those paths, it cannot live in the workflow definition, because those paths do not run it. A validation expressed as a condition in a transition is enforced for exactly one route into the data and silently absent from every other.
This is the reason so many systems have data that violates their own stated rules. The rule was real, it was implemented, and it was implemented in a place that only one entry path passes through.
The remaining questions
- Who needs to change it? If the answer is a business user, it cannot live in code, and that decision is made at design time or not at all.
- How often does it change? A rule that changes with every policy cycle belongs in data; a rule that has not changed in a decade belongs in code.
- Must past cases be reproducible? If yes, the rule needs versioning, which favours a home that can record which version applied.
- Can it be tested without starting the engine? Logic that can only be exercised by running a workflow instance will, in practice, not be tested.
- Is it about who does something, or about what is true? Assignment and routing are workflow concerns; truth about the data is a domain concern.
The fourth question deserves more weight than it usually gets. Testability is not a nice-to-have here - it is the property that determines whether the rule can be changed confidently a year later, and it is almost entirely decided by where the rule lives.
What actually belongs in the workflow definition
A workflow definition earns its place by doing what nothing else does well: coordinating people over time. That is a short list, and keeping it short is the point.
- The sequence of stages and the transitions between them.
- Who is assigned each task, and what happens if nobody acts.
- The named outcomes available at each stage.
- Conditions that choose a path - expressed as calls into services, not as embedded logic.
- Notifications tied to stage entry and exit.
Everything else - what makes a submission valid, what an amount should be, whether a document is acceptable, what the applicant is told - is domain logic that the workflow invokes rather than contains.
Signals a rule is in the wrong place
| Signal | What it usually means |
|---|---|
| Scripts inside the definition growing past a few lines | Domain logic has migrated into the engine |
| The same condition copied into several transitions | It should be one service method |
| A policy change requires redeploying the definition | Values are embedded where logic belongs |
| Data created by import violates a workflow rule | The rule only guards one entry path |
| Nobody can answer a what-if without reading the diagram | The rule is not expressed anywhere testable |
| The workflow is versioned to change a number | The number should be configuration |
The opposite mistake is real too
Advice to keep logic out of the workflow gets over-applied, and the result is its own problem: a service layer that hand-rolls a status field, a queue, an assignment table, and a set of reminder jobs. That is a workflow engine, written badly, without history, delegation, or an audit trail.
Sequencing and human coordination are genuinely hard, and they are what the engine is for. A team that finds itself building status transitions in Java has usually mis-diagnosed the original problem: the fix was to move the domain logic out of the definition, not to abandon the definition.
Moving logic without a rewrite
Existing applications rarely need a redesign to correct this. The mechanical version works well: take the script inside a transition, move its body verbatim into a service method, call the method from the transition, then write tests against the method. The behaviour is unchanged at every step, and the rule becomes testable and reusable in a single move.
Once extracted, the second question becomes answerable: is this rule stable enough to stay in code, or volatile enough to deserve externalised, versioned rules? That is a decision worth making deliberately, and it is much easier to make about a service method than about a fragment of script inside a diagram.
Conclusion
Placement is not a matter of taste. Ask whether the rule must hold outside the workflow, and most cases answer themselves - anything that governs the data belongs with the data, and the workflow calls it. Keep the definition to coordination: sequence, assignment, outcomes, notification. Keep domain truth in services where it can be tested and reused. And when the rule is really policy rather than logic, recognise that it wants a home that is not code at all.