Subflows do not allow for msg values from previous nodes to be used when setting properties

As @Mecworks asked on Subflows do not allow for context variables to be used when setting properties. Why? about using context variables to fill the values of subflow properties, I want to know how can I use the value of a previous node msg as the value of a subflow environment variable property, something like this:

I’m using subflows turned on custom nodes, and the idea here is to config that subflow (now a custom node) environment variables with the values that are coming from a previous msg in the main flow.

I know that I can use for example the msg.payload that is coming from the last node connected to the subflow/custom node, but is far more clear if I can define the values that the subflow “expect” to work in this situation (msg.payload.value1, msg.payload.value2, etc.), and my idea is to equal different environment variables to different msg.payload.* values (or fill the values manually, that I already can in this situation), and then, use the environment variable instead of msg.payload.* inside the subflow/custom node.

Is there a workaround to define environment variables for the subflow/custom node from msg values of a previous node?

Thanks in advance for your help.

Hi @aguida79

What you have marked as the solution won’t work as you expect.

Env vars are evaluated (and their values fixed) when the flow starts. They cannot be dynamically changed by either context or msg properties.

Thanks @knolleary for your answer. I unchecked my answer as “Solution”, and delete the answer, to not confuse to anybody.

So, there is no workaround for what I’m trying to achieve?

Thanks again for your help.

Hi @aguida79

What you should do in this instance,
Is use the option for your subflow as a default only .

Whenever a msg arrives on that subflow that contains an alternative value, set a flow variable inside that subflow to this new value

Then any nodes that needs to use it inside the subflow, just do a logical OR expression.


const SomethingThatNeedsIt = flow.get(“override”) || env.get(“default”)

I’m on the phone - so hopefully my description makes sense

Thanks for your reply @marcus-j-davies .

I ended doing a logic inside the subflow with a function, to check if the env variables are completed or no. If yes, I replace the msg that is coming from the previous node with the value of the env variable:

With that, I allow the user to complete the needed values in the subflow/custom node properties, or pass them in the msg, because I’m using the name of the required messages as labels, so the user knows the needed “msg names”:

I’m going to add an additional logic to answer with an error if not all the required msg names / env variables are provided, but with this approach, I can accomplish my use case.

Thanks to all for your help.