All posts
Liferay · 11 min read · Oct 23, 2026

When Should Business Logic Live in Workflow vs Java Code?

Choosing between workflow definition and service layer for business logic

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

HomeNaturally good forBad forChanged by
The workflow definitionRouting, assignment, stage sequenceCalculation, validation, anything reused elsewhereA developer, via redeploy
The service layerDomain rules, validation, calculation, integrationSequencing and human task assignmentA developer, via release
A rules engineVolatile policy with many conditions and variantsSimple or rarely changing rulesAn analyst, with governance
Declarative model configurationField validation, allowed values, permissionsAnything conditional across entitiesAn administrator, at runtime
Each home differs most in who can change it and what that change costs.

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

  1. 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.
  2. 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.
  3. Must past cases be reproducible? If yes, the rule needs versioning, which favours a home that can record which version applied.
  4. 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.
  5. 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

SignalWhat it usually means
Scripts inside the definition growing past a few linesDomain logic has migrated into the engine
The same condition copied into several transitionsIt should be one service method
A policy change requires redeploying the definitionValues are embedded where logic belongs
Data created by import violates a workflow ruleThe rule only guards one entry path
Nobody can answer a what-if without reading the diagramThe rule is not expressed anywhere testable
The workflow is versioned to change a numberThe 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.

Contact

Let’s connect

Have a Liferay or Backend project you’d like to talk through? Fill in the form and I’ll get back to you.

Book Calendar