Setting values to variables while starting process instance

I was using camunda REST API.

I want to use certain values later in different workflow tasks. So i want to pass the values from external applications and set it to camunda variables while starting process instance or at any stage of handling usertask.

I referred the “generic form” of user task, will it helpful? if yes, how can we use it. Or any other approach to set variables?

1 Like

You can pass variables into a process when starting using the REST payload sent that actually starts the process (assuming that’s how it’s starting). Payloads are JSON that look like this:

{
    "variables":
    {
        "myFirstVar": {"type": "string", "value": "I am a value"},
        "mySecondVar": {"type": "integer", "value": 2},
        "myThirdVar": {"type": "boolean", "value": true}
    }
}

This payload will automatically create three process variables.

The second method, and one I use, is to use a “start” listener on the start event. This listener should be a “Script” of type “Groovy”. The body script itself would then consist of a series of statements, each of which creates a process variable and sets its value:

execution.setVariable("myFirstVar", "I am a value");
execution.setVariable("mySecondVar", 2);
execution.setVariable("myThirdVar", true);

Camunda automatically “types” your values for you.

Hope this helps.

So these variables will appear in the Camunda Tasklist web apps “Generic Task Forms” or “Embedded Task Form” ?

And also I found two different variables in addition to business key in a Generic task form,

  1. Add a Variable
  2. Local Variable

What is the difference between these two fields and in which scenario we have to use those variables? (img attached for reference)

Unfortunately, I have almost no experience with forms. I would guess that the form is offering you the option of “manually” creating these variables. The following documentation may help you further:

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