Flow variables like Workflow

Hi

In workflow you can add local variables and able to change them throughout the process.

How would you initialize these vars in automate for example the trigger is a folder capture node?

Im aware of the flow environment variables. Is there an equivalent in Automate the way Workflow has local variables?

Thank you
E

Hi Edanting,

Msg properties can be used in a similar way to local variables.

You can set or update msg properties using a Function node or a Change node. The Change node is probably closest to Set Job Infos in Workflow. There is no need to define these properties upfront.

For example, this can be linked directly after a Folder Capture node:

msg properties are only available to the nodes in the respective flow.

Flow variables are available to all flows/processes on the tab.

Hope this explains it.

Erik

1 Like

Thanks Erik. Im actually learning towards something like this but was hoping it would be a bit different in Automate. For example an init script triggering before the folder capture and folder capture can use the value from the initialized variables.

Anyways thank you
regards

This is best addressed using environment variables.

Did you use the OL Connect Automate installer for your installation?

When using the installer, you should find a file named olca.environments.json in the following location:

C:\ProgramData\Objectif Lune\OL Connect Automate

In this file, you can define environment- or local-machine-specific variables. The file is JSON-based and uses key-value pairs, where each key is the name of an environment variable that can be used in your flows.

For example:

{
  "ENV_INSTANCE": "olca-local-dev",
  "ENV_WORKSPACE": "C:\\workspace"
}

Note the use of double backslashes in the path. These are required to escape backslashes correctly in JSON.

These variables can then be used in Change nodes, Function nodes, JSONata expressions, and other places in your flows.

For example, in a Folder Capture node, you can use JSONata to set the path to an in folder like this:

$env("ENV_WORKSPACE") & "\\letters\\in"

This basically results in:

C:\\workspace\\letters\\in

On another machine, the workspace could be located somewhere else, for example:

D:\\automate\\some_other_folder\\letters\\in

Other example usage:
Reading the env var to a variable in a Function node:

let workspace = env.get("ENV_WORKSPACE")
msg.targetFolder = `${workspace}\\letters\\out`

// Or:
// msg.targetFolder = env.get("ENV_WORKSPACE") + "\\letters\\out"

return msg;

This approach lets you keep your flows portable across different machines while only changing the environment-specific configuration.

I hope this helps,

Erik

Hi Erik,

Thanks for this. Yes we did use installer to install the program. And also am aware of the olca.environments.json . We actually want the env vars to be system only info, instead of having multiple entries for specific flow.

I was hoping for a script unique to a flow to run before the actual process has triggered. So you can dynamically set the capture folder path based on certain requirements. I guess this is a Node-Red limitation where there olca.environment.json do fulfil that requirement but it becomes globally available to other flows which it doesnt need.

Anyway I do have another question, the global include.json in Connect Workflow is a god send. Is there away we can do this in Connect Automate? From my search you can do something like this:

functionGlobalContext: {
    text: require("./lib/text"),
    data: require("./lib/data"),
    api: require("./lib/api")
}

By modifying settings.js. Im currently trying it now if this is achievable.

Thank you
Kind regards

You mean loading custom modules for the Function node?

If you want to load a custom module into the global context, you’re on the right track. You can register it in settings.js using functionGlobalContext:

functionGlobalContext: {
    olUtils: require('./lib/olUtils.js')
},

Here’s an example module to test with:

module.exports = {

    formatJobName: (name) => {
        return name.trim().toLowerCase();
    },

    onePlusOne: () => {
        return 1 + 1;
    }

};

Then in any Function node, access it via the global context:

const olUtils = global.get('olUtils');
node.warn(olUtils.onePlusOne()); // outputs 2 ;)
return msg;

Note: OLCA introduced olca.settings.json as a way to configure the underlying Node-RED without modifying settings.js directly, which keeps things safe during upgrades. However, olca.settings.json does not currently support require(), so loading custom modules this way still requires a manual edit to settings.js.

Thanks Erik! This works!

One thing to point out that whenever you modify the script, you will need to restart OL Connect Automate service for the changes to take effect.

One work around similar to Connect Workflow loading of global includes in runtime:

// loader.js
const path = "c:\\my_includes";

exports.load = function(name) {
    const file = `${path}\\${name}.js`;
    delete require.cache[require.resolve(file)];
    return require(file);
};

And in settings.js:

    functionGlobalContext: {
        loader: require('c:\\my_includes\\loader.js')
    },

And to use it in function node like so:

const loader = global.get("loader");
const text   = loader.load("text"); // load the file "text.js"

node.warn(text.toLowerCase("HELLO WORLD"));

return msg;

Cheers

1 Like

You could even consider creating your own Node-RED node at some stage :wink:

1 Like