How to add a var to newly created instance?

Hi!

I’d want to add some variables to all just created process instances on runtime. How to?
SpringBoot webapp is in use.

If it does matter, here are some details: I’d want to make current Spring active profile list accessible for js scripts in embedded forms.

I have found an example with parse listener where we add execution listener to get some info from Service Task: camunda-bpm-examples/process-engine-plugin/bpmn-parse-listener at master · camunda/camunda-bpm-examples · GitHub
But I want to put some info into User Task :slight_smile:

Hi @student975,

if you want add a new variable, you can use this rest api call: Put Process Variable | docs.camunda.org.

To map existing variables to another name, you can use the input mapping in the modeler: Process Variables | docs.camunda.org.

Hope this helps, Ingo

1 Like

Ingo, sorry, can not understand. The aim is to ad a var on process instance creation on the server side and to read this var from js inside embedded form (that is SPA side) with camForm.variableManager. While REST API presumes putting the var in opposite direction (that is ajax call from SPA to REST). Or I have misinterpreted your answer :slight_smile:

Hi @student975,

sorry, this was not clear to me.

If you write execution.setVariable("name", someValue); in a listener, you can set process variables on each available event.

Hope this helps, Ingo

Do you mean DelegateExecution will work for User Task or Start Event (embedded form holder) elements also? I guessed it takes place for Service Tasks only.

Hi @student975,

yes. have a look at Delegation Code | docs.camunda.org

somewhere in the middle it states

It is also possible to use a delegation class that implements the org.camunda.bpm.engine.delegate.JavaDelegate interface.

Hope this helps, Ingo

Ingo, thanks for your support! After some battles have got a code working for User Tasks:

@Component
@Order(Ordering.DEFAULT_ORDER + 1)
class SpringProfileInjector : AbstractProcessEnginePlugin() {

    @Autowired
    private lateinit var springProfileInjectorParseListener: SpringProfileInjectorParseListener

    override fun preInit(processEngineConfiguration: ProcessEngineConfigurationImpl) {
        processEngineConfiguration
            .customPreBPMNParseListeners
            .add(springProfileInjectorParseListener)
    }
}

@Component
class SpringProfileInjectorParseListener : AbstractBpmnParseListener() {

    @Autowired
    private lateinit var camundaFormListener: CamundaFormListener

    /* Doesn't work for now (at list for 7.10 version)
    override fun parseStartEvent(element: Element, scope: ScopeImpl, activity: ActivityImpl) {
        parse(element, activity)
    }
     */

    override fun parseUserTask(element: Element, scope: ScopeImpl, activity: ActivityImpl) {
        parse(element, activity)
    }

    private fun parse(element: Element, activity: ActivityImpl) {
        element
            .attributes()
            .any { it.endsWith(":formKey") }
            .let {
                activity.addListener(ExecutionListener.EVENTNAME_START, camundaFormListener)
            }
    }
}

@Component
class CamundaFormListener : ExecutionListener {

    @Autowired
    private lateinit var environment: Environment

    private val profiles by lazy {
        environment
            .activeProfiles
            .joinToString(",")
            .let { it.ifBlank { "profiles not found" } }
    }

    override fun notify(execution: DelegateExecution) {
        execution.setVariable("spring-profiles", profiles, execution.currentActivityId)
    }
}

For Start Event it doens’t work for some reaason, while notify() is called.