Search code examples
javavariablesscopebpmncamunda

How to set global process variables in Camunda-BPM?


I have a simple bpmn process in which i am using 2 service task,I am executing my process by using processEngine.getRuntimeService().startProcessInstanceByKey("Process_1", variables); where my variables is as follows:

Map variables = new HashMap();
variables.put("a", 2);
variables.put("b", 5);

Service task 1 implements an Addition java class and service task 2 implements a Multiplication class.

Now I want to have 3 variables (constants) c = 5, d = 10, e = 2 so that I can use c for service task 1 such that in Addition I can use this variable, similarly I want to use d in my Multiplication class, and e should be global so that I can use it in both classes.

Can anyone guide me on this?


Solution

  • As a quick fix you could include a Setup-Service Task as the first Task of the process which prefills your process-variables. Depending on how you start a process you could either: Set the Variables via the java-object-api

    https://docs.camunda.org/manual/7.5/user-guide/process-engine/variables/#java-object-api

    or you if you use a REST call you can provide these fixed values within the request body:

    https://docs.camunda.org/manual/7.5/reference/rest/process-definition/post-start-process-instance/

    Another simple solution would be a class with static values or a enum holding the needed values.

    --edit--

    if you want to use the inputOutput extension add something like this to your bpmn file:

    <bpmn:process id="Process_1" isExecutable="false">
      <bpmn:extensionElements>
        <camunda:inputOutput>
          <camunda:inputParameter name="c">5</camunda:inputParameter>
          <camunda:inputParameter name="d">10</camunda:inputParameter>
          <camunda:inputParameter name="e">2</camunda:inputParameter>
        </camunda:inputOutput>
      </bpmn:extensionElements>
    

    this can't be done in the diagram view of the camunda modeler, just switch to the XML representation of the process and add the extensionElement.