How to update a process instance variables?

I need to update the process instance variable(s) of an in-flight (is it considered inflight until the process terminates?) process from another workflow but I am not sure the best way to go about it as I am still new to flowable. The process itself is started via a message start event by a different Workflow process with a businessKey that is derived from a unique property in the process variable.
The two ideas I have are to:

  1. Add a catching intermediate event to the process definition and then throw a message event signal with the updated properties from the process that needs to update the process instance variables:

void messageEventReceived(String messageName, String executionId, HashMap<String, Object> processVariables);

  1. Retrieve the process instance through a ProcessInstanceQuery then update the process variables as needed.

I’m not sure if there is another better way to go about this. I am aware of the following with regards to my two options:
1 - Going this route would mean that every process instance would “wake up” to catch the eventSignal and would thus have to implement checks so the only the process instance with the right property updates using the provided variables. This could get expensive if there are 100s of this process instances in-flight

2 - Seems a bit more cumbersome than option 1, also I’m not sure if updating the process variables for the instance will bring it out of the suspend state.

Side note: By the way I think it’d be quite nice to be able to retrieve process instances by businessKeys.

What you’ll probably want to do is retrieve the process instance as you describe in idea 2, then trigger it with a message as you describe in idea 1. ProcessInstanceQuery has processInstanceBusinessKey() to find process instances on business key.

Your assumption 1 is incorrect: messages are not broadcast, but targeted at specific executions. Note there is parameter executionId on messageEventReceived. Signals OTOH have broadcast semantics. Regarding point 2: updating variable values does not trigger execution.

Supposing that the execution is not actually suspended, but rather at a ‘wait state’ such as an intermediate message catching event, triggering a message event is precisely what you would do.

2 Likes

If I retrieve the process instance via the query, wouldn’t it be easier to just update the process variables itself as opposed to using a signal to message it afterwards? I assume once the process variable are changed the outgoing sequence flows are evaluated and process moves to the next execution.

My apologies. The process variables can not be set as I had wrongly assumed again.

No, updating variables does not trigger the process engine. You could indeed set the process variables now that you have the execution, but you still need to do the triggering.

If the wait state is an intermediary message catch event, the trigger would be a message. If it is a signal catch it would be a signal trigger. If it is a user task, it would be the completion of the task.

1 Like