= How a flownode is executed
:description: A flownode execution can come:

A flownode execution can come:

* from an API call
* from a specific work triggered by the Engine internally

== API call

When the call comes from the API (generally a human task executed from an application), some verifications are done, contract data are checked and stored in database, then a work is triggered to execute the flow node. +
See the xref:work-execution.adoc[work page Work-execution] for details.

== Internal engine trigger

Works are automatically triggered when:

* The process starts it.
* The previous node of the process finishes.
* The previous flownode state starts it.

== Execution of a Flownode State

The execution logic of the flownode (ie. what the flownode tries to do) depends on the State of the flownode.
Flownode execution happens in a work. The page [[Work execution|Work-execution]] explains the operations done by the wrapped works. After they are done, the parent work, ExecuteFlowNodeWork, executes its own logic:

* It verifies the flownode is in the required state and has not changed since the start of the execution. If it has, a SWorkPreconditionException is thrown.
* The thread classloader is set to be the process classloader.
* It executes the current state, running the `execute()` code of the State implementation. Eg. ExecutingCallActivityStateImpl.java
* If the execution of the state is finished (unlike when executing connectors, where the state remains the same), the state of the flownode is updated with the next state that should be executed. (*)
* Then, if the next state requires to be executed as well, we register a work to go on (execute next flownode state if state is not stable, or notify child finished if state is terminal).

(*) to determine the next state, we execute the `shouldBeExecuted()` code of each state (defined by the state transitions) until one returns true. This code may executed things, not only return a boolean. If it returns true, we stop and update the state of the flownode with that one.
