Create global process variable without attaching it to a bpmn node using java code

Description

Create global process variable without attaching it to a bpmn node using java code:

the process variable to be attached to process id and it should be used globally.

I do not want to attach process variable to a node.

Requirement:

Create global variable during process creation and these variables shall be available while process execute. we need to create process variable using java code and attach these variables to process id.

We tried below solution so far that is not working and value of process variable comes null, when we try to access is.

Not working solution1:

ExtensionElements extensionElements = modelInstance.newInstance(ExtensionElements.class);
{{ ZeebeOutput outputParameter = modelInstance.newInstance(ZeebeOutput.class);}}
{{ outputParameter.setSource(“globalVarValue”);}}
{{ outputParameter.setTarget(“globalVar”);}}

{{ extensionElements.addChildElement(outputParameter);}}
{{ serviceTask.setExtensionElements(extensionElements);}}

Issue: value of global variable is null when we want to access in in bpmn during run time.

Not working solution2:

// Create and configure the extension elements
{{ ExtensionElements extensionElements = modelInstance.newInstance(ExtensionElements.class);}}
{{ ZeebeProperty zeebeProperty = modelInstance.newInstance(ZeebeProperty.class);}}
{{ zeebeProperty.setAttributeValue(“globalVar”,“globalVarValue”);}}
{{ zeebeProperty.setName(“globalVar”);}}
{{ zeebeProperty.setValue(“globalVarValue”);}}

{{ // Add the property to the extension elements}}
{{ extensionElements.addChildElement(zeebeProperty);}}

{{ // Add the extension elements to the process}}
{{ process.setExtensionElements(extensionElements);}}

Issue: value of global variable is null when we want to access in in bpmn during run time.

Why we do not want to attach global process variable in a bpmn node?

Answer: This as per my requirement. Later I can tell more if needed.

Do we have a hidden bpmn element where we can attach process variable. if its not possible to have global process variable without attaching it to bpmn element.

I would recommend starting simple, and working up to what you’re trying to do.

  1. Create a BPMN model using modeler and deploy it to your Zeebe engine.
  2. Using Operate, start an instance of the model with variables, confirm that they are present with Operate
  3. Using the Java SDK, start an instance of the model with variables, confirm that they are present with Operate
  4. Using the Java SDK, start an instance of the model with variables, confirm that they are present with the Java SDK

This solution is not related to my problem. My Challenge: I want to create global process variable using java code.

@Raj_Kishun_Singh - another user asked the same question, but @GotnOGuts has the correct approach. You can start a process instance with variables, which are scoped to the process itself. You can also use the SetVariables gRPC endpoint and for the element key use the process instance key, which achieves the same result.