Flow Set $parent.<Dynamic Name>

Hello,

I'm storing the flow context in the parent for a subflow.

I need to name the variable dynamically based on a login name.

let userName = env.get("USERNAME");
if (msg.hasOwnProperty('UserName')) userName = msg.UserName;

let wSession = {};

wSession.session = "JSESSIONID=" + msg.payload.result.sessionID;

msg.wSession = wSession.session

flow.set("$parent.userName", wSession );

This does not work of course, the flow is stored against the property userName and not the value of userName.

Thanks
Harry

Something like

flow.set(`$parent.${userName}`, wSession )

You could rewrite the first lines as
const userName = msg.UserName ?? env.get("USERNAME")

and, personally I would also use

wSession.session = `JSESSIONID=${msg.payload.result.sessionID}`

Note that you don't generally need semi-colons.

1 Like