Using process instance id as correlation key for intermediate catch events

@jonathan.lukas @mho
Hi all,

I have a similar problem.

In Camunda 7 there was ability to send the process instance id during message correlation:

runtimeService
                .createMessageCorrelation(MESSAGE_NAME)
                .processInstanceId(processInstanceId)
                .setVariables(variables)
                .correlateWithResult();

If I understood Lukas answer good, the above code can be written in Camunda 8 like this:

        Map<String, Object> correlationKey = Map.of("MESSAGE_CORRELATION", processInstanceId);
        return zeebeClient.newPublishMessageCommand()
                .messageName("MESSAGE_NAME")
                .correlationKey(correlationKey.get("MESSAGE_CORRELATION").toString())
                .variables(correlationKey)
                .variables(variables)
                .send();

EDIT:

After testing I noticed that variables cannot be send at the same time as message, i.e. all variables which is used by message boundary event should be defined before token arrives to (wait for) that event (which is expected):
- Error type: EXTRACT_VALUE_ERROR
- Error message: failed to evaluate expression ‘MESSAGE_CORRELATION’: no variable found for name ‘MESSAGE_CORRELATION’

My question is what is the best practice, where to define correlation key variables?
In the service task immediately after process is started?