How can i Read Parent Process Instance Key from Sub ProcessInstance key(Call Activity) in modeller or Sub ProcessInstance Job worker
Hi @Aparnaiaai
So what are you trying to do exactly?
@Aparnaiaai In Camunda 8 (self-managed), when you have a Call Activity that starts a child process, the child process inherits some context from the parent, including the Parent Process Instance Key.
To get the Parent Process Instance Key from the child process within a Job Worker, you can access the parentProcessInstanceKey
from the ProcessInstanceRecord
—but in a Job Worker, this information is not directly passed in the job payload by default.
However, here are the approaches:
Approach 1: Use ZeebeClient.newProcessInstance()
metadata via Operate or via a custom interceptor
Camunda stores this information internally, and Operate UI can show it. But to access it programmatically inside a Job Worker, you’ll need to:
Pass parentProcessInstanceKey
explicitly as a variable
In the parent process, pass the processInstanceKey
to the child like this:
<callActivity id="CallActivity_1" calledElement="child-process">
<extensionElements>
<zeebe:ioMapping>
<zeebe:input source="= processInstanceKey" target="parentInstanceKey" />
</zeebe:ioMapping>
</extensionElements>
</callActivity>
Then, in the child process, this variable parentInstanceKey
is available in your Job Worker.
Approach 2: Use the Zeebe Java Client to fetch process instance metadata
This is only possible if you’re storing process instance keys and querying the data via Operate API or Zeebe Elasticsearch exporter. But in the job handler, it’s not available by default, unless you do custom exporting.
Recommended approach (for Job Workers):
Pass the parent processInstanceKey as a variable when calling the child process. Here’s how you do that in Java:
zeebeClient
.newCreateInstanceCommand()
.bpmnProcessId("child-process")
.latestVersion()
.variables(Map.of("parentInstanceKey", context.getProcessInstanceKey()))
.send();
But since you’re using a Call Activity, just map the variable directly using Zeebe IO Mapping as shown in Approach 1.
@Aparnaiaai Here’s a Java code snippet for how you can access the parentInstanceKey
inside your Job Worker once you’ve passed it from the Call Activity using input mapping.
Java Worker Snippet
import io.camunda.zeebe.spring.client.annotation.ZeebeWorker;
import io.camunda.zeebe.spring.client.job.ZeebeJob;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class ChildProcessWorker {
@ZeebeWorker(type = "your-task-type")
public void handleChildJob(final ZeebeJob job) {
Map<String, Object> variables = job.getVariablesAsMap();
Long parentInstanceKey = null;
if (variables.containsKey("parentInstanceKey")) {
parentInstanceKey = Long.parseLong(variables.get("parentInstanceKey").toString());
}
System.out.println("Parent Process Instance Key: " + parentInstanceKey);
// Optionally update variables or complete the job automatically (if auto-complete is off)
}
}
Make sure this is in place in your BPMN
In the Call Activity, set this input mapping:
<zeebe:input source="= processInstanceKey" target="parentInstanceKey" />
Your child process will receive the parent instance key as a normal variable, and you can use it as needed.