I have an elsa workflow parent which is calling some child workflows.
Every child workflow has some custom activities (based on SendHttpRequest elsa activity).
These custom activities need to call an external api and provide the workflowInstanceId of parent workflow.
I tried to pass the workflowInstanceId of parent as ContextId of child workflow – not working since is overridden inside of child workflow custom activity (which makes sense of course).
How could I get (server-side) the workflowInstanceId of parent workflow from a piece of code which is running on a child workflow
(let’s say from OnExecuteAsync(ActivityExecutionContext context) of child workflow custom activity)?
Although Elsa 2 (2.6 at the time of writing) does not have a real notion of parent-child workflows (i.e. workflows invoked by other workflows do not have any reference to their parent), you can use the
RunWorkflowactivity to provide arbitrary input to the child workflow. Including its own workflow instance ID.For example, use the following JS expression on the
RunWorkflowactivity to send the workflow instance to the child workflow:Then, from your child workflow, your first activity should be the one to capture this value, e.g. using a
SetVariableactivity:Now that you have the parent workflow instance ID stored in a workflow variable, you can access it from anywhere in your child workflow.
Here is an example of a parent workflow you can import via the designer:
And here is the Child you can import:
Once imported, make sure both workflows are published. You can then invoke the parent workflow via its HTTP endpoint, e.g.:
https://localhost:11000/parentWhich will result in a response similar to this:
This response is generated by the Child workflow (which successfully received the parent workflow instance ID)