OL Learn

Run script external javascript library

Hello,

So I was writing a run script using JavaScript and was wondering if it was possible to use an external library in the workflow scripts. I found the below link on how to use an external js library in the data mapper but was unable to find one on how to do this in the workflow.

http://help.objectiflune.com/EN/pres-connect-user-guide/1.6/datamapper/Scripting/Writing_Scripts.htm

In the actual version of Javascript that is used by the Run script plugin (which really is the JScript of Windows), you cannot call external libraries…BUT you can create you own API that can call function from the Run script plugin.

Please message me in private and I will returned you a PDF explaining how to achieve this. It is a post from one of our “guru” on our internal forum.

Hi,

I’m trying to figure out how to write the contents to a string to a file in workflow using the javascript run script but everything I’m finding says I need to use something like this:

const fs = require('fs');

fs.appendFile('message.txt', 'data to append', function (err) {
  if (err) throw err;
  console.log('Saved!');
});

but it doesn’t seem to like the “const” command. Is this linked to this?

The code you are referencing is specifically for NodeJS, it cannot run as-is in Workflow.

The easiest way to create a file with Workflow’s JavaScript is… to not do it from inside the script!
Instead, store all the values you want to write to a file inside a variable or a Job Info. For instance:

Watch.SetVariable("myVariable", "This is the text to write")
or
Watch.SetJobInfo(9,"This is also some text to write")

Then, immediately after your script, use a Create File task that only contains your variable:
%{myVariable}
or
%9

After that, you can use a Send To Folder task to store that newly created file anywhere you want.

Note that it’s also possible to write to a file directly from inside the script, but it requires a bit more code and the above method is usually more than adequate.