How do I merge two contexts in a process using FEEL?

Kristoffer Bakkejord: Is there support in Zeebe/feel-scala to merge two contexts? In JavasScript it can be done like so:

const object2 = {age: 35}
const object3 = {...object1, ...object2 }

What I’m looking for is to merge two contexts, before sending it to the worker.

Josh Wulf: Parallel gateway join? https://docs.zeebe.io/bpmn-workflows/parallel-gateways/parallel-gateways.html

Kristoffer Bakkejord: To explain further, task 1 creates a dictionary I would like to have merged with some static variables (provided when creating the instance, or hard coded in the workflow).

So in the JS example above, I might have a worker that outputs object2 (JSON object with age). I would like that merged with object1 (name), before sending the resulting object ({"name": "Flavio", "age": 35}) to a worker.

korthout: > before sending to the worker
this sounds like a case for input mappings
https://docs.zeebe.io/reference/variables.html#inputoutput-variable-mappings

korthout: for merging 2 variables, have a look at the 3rd example in https://docs.zeebe.io/reference/variables.html#input-mappings
This creates a new variables with properties for both original variables, but you can use the same idea to overwrite properties of (or add properties to) an existing variable

Kristoffer Bakkejord: @korthout It seems close, but not sure it’s quite what I need, since I then need to know the exact values that will exist. I will investigate this a bit further.

korthout: Do you want the object 3 to contain flattened object1 and object2, or is it fine to have object3= { "object1":{…}, "object2":{…} }? I’m not sure Zeebe is capable of flattening a structure without knowing the properties, perhaps @zell knows?

Kristoffer Bakkejord: I would like object3={name: 'Flavio', age: 35}

Kristoffer Bakkejord: Seeing that this issue is unresolved, I suppose this isn’t going to work the way I want it to: https://github.com/camunda/feel-scala/issues/169

korthout: Have you tried using something like the following, and just see what happens?

source: =object1
target: object3

source: =object2
target: object3

Kristoffer Bakkejord: Will need to try that out :slightly_smiling_face:

Kristoffer Bakkejord: The issue I linked to does mention this:
> Zeebe contains a similar function that is used for the (output) variable mappings.

korthout: Yup :wink:

korthout: I’m just not entirely sure, and if it works it’s also undocumented behaviour I believe :laughing:

zell: this will overwrite the object 3

zell: https://github.com/zeebe-io/zeebe/issues/297

zell: As @saig0 suggested there you could use feel functions like append

Kristoffer Bakkejord: There exists a JavaDoc for this, but it should perhaps be put in the Zeeebe documentation as well?

https://github.com/zeebe-io/zeebe/blob/82974662e7e86d9c9f7257019f3a9501a723b21f/engine/src/main/java/io/zeebe/engine/processing/deployment/model/transformer/VariableMappingTransformer.java#L22-L65

korthout: Thanks for bringing this up, I’ll make sure to take a look at this :wink:

Kristoffer Bakkejord: I commented with another example that can be used in the docs: https://github.com/zeebe-io/zeebe/issues/297#issuecomment-734393279z

korthout: Thanks, that’s very helpful :heart:

Note: This post was generated by Slack Archivist from a conversation in the Zeebe Slack, a source of valuable discussions on Zeebe (get an invite). Someone in the Slack thought this was worth sharing!

If this post answered a question for you, hit the Like button - we use that to assess which posts to put into docs.