DataMagik Script Engine
What the Script Engine is
The Script Engine runs small JavaScript programs, called scripts, inside DataMagik. A script can read from Plex, call another company's web service, make PDFs and labels, send email, write files and return a result to whoever started it.
Scripts run in DataMagik's own sandbox, on DataMagik's servers. You do not install anything. A script sees only your company's data, and it reaches other systems only through the tools DataMagik gives it, such as plex.execute, odbc.executeQuery, documents.generateSync, email.sendWithTemplate and fetch. The full list is in the Script API reference.
Every script has a main(context) function. context holds the input for this run, and what main returns is the result.
Scripts or workflows?
A workflow is the same kind of automation built by connecting steps on a canvas instead of writing code. When you save a workflow, DataMagik turns it into a script, so schedules, API calls, run history and everything else on this page work with workflows too.
| Build a workflow when… | Write a script when… |
|---|---|
| You do not write JavaScript, or the people who look after the automation do not. | You are comfortable in JavaScript. |
| The job is a chain of steps the workflow palette already has: Plex, files, Excel, documents, labels, email, approvals, AI. | The logic is mostly calculation or many nested decisions. |
| You want to test step by step and see each step's data. | You need a Script Engine feature that has no workflow step. |
You can mix the two: a workflow can run a script, and run a few lines of JavaScript in a Code step. See Workflows and Workflows and JavaScript.
Finding your way around
Everything is under the Script Engine menu, which people with the DataMagik - Builder permission see.
| Menu item | What it is for |
|---|---|
| Scripts | The script editor: find, write, test, save and configure scripts. See Writing scripts. |
| Data for Excel | Every script your company has published to Excel, with its link. See Scripts in Excel & Power BI. |
| SQL Editor | Write and save the Plex SQL queries that scripts run with odbc.executeQuery. See SQL Editor. |
| Plex Data Sources | Browse the Plex data sources that scripts call with plex.execute. |
| Credentials | Passwords, API keys and tokens that scripts use, stored once for the whole company. See Script credentials. |
| Git | Your own Git access, for keeping scripts in a repository. See Git integration. |
| Schedules | Run scripts by the clock. See Script schedules. |
| Executions | Every run in your company: what ran, what started it, how it ended, what it logged and what it returned. See Script executions. |
| Integrations | Groups of scripts that work together, such as one partner's integration, watched as one: their health, traffic and failures. See Integrations. |
| Workflows | The visual builder. See Workflows. |
| Inbound Email | Hosted email addresses that run a script when mail arrives. See Inbound email. |
| Email Templates | Saved emails that scripts send with email.sendWithTemplate. Shown only to people with the Email Management permission. See Email templates. |
- People with DataMagik - Viewer can open Executions, Integrations, Workflows (read-only) and Data for Excel. They have no Script Engine menu, so Data for Excel appears in their main menu instead.
- Credentials can also be opened with DataMagik - Security.
- Creating and changing Inbound Email addresses needs DataMagik - Security.
Your first script
- Open Script Engine › Scripts.
- Click the + button (New Script) in the toolbar. A script named New Script is created with some starter code and opens in the editor.
- Click the Script settings button, change Script Name to something that says what it does, add a Description, and click Save Changes.
- Replace the code with this:
function main(context) { const part = context.part_no || "unknown"; console.log("Checking part", part); return { success: true, script_message: "Checked part " + part, data: { part_no: part } }; } - Click the Context browser button and, on the Test input tab, type the input for the test:
{ "part_no": "44-9912" }. - Click Test. The script runs the code in the editor, saved or not, with your test input. The Execution history panel opens and shows the run: Console Logs holds the line written by
console.log, and Return Value holds whatmainreturned. - Click Save (or press Ctrl+S). Each save makes a new version, and that version is what schedules, webhooks and every other real run use from then on.
Test does real work. If your script sends an email or writes to Plex, a test run sends or writes it too. Point emails at yourself while testing, or use Debug, which captures emails instead of sending them. Writing scripts covers testing in full.
What starts a script
The same script can be started from many places. Each one hands the script a different context, and reads different parts of what it returns. Every run also gets context.company_id, context.user_id and context.triggered_by, which says what started it.
| Started by | When | Read more |
|---|---|---|
| Test, Debug and Run in the editor | You press the button. Test and Debug run the code in the editor; Run runs the saved version. | Writing scripts |
| A schedule | On a timetable, such as 07:00 every weekday. | Script schedules |
| A webhook or API call | Another system sends a request to the script's address with an API key. The Webhook button in the editor shows the address. | Script webhooks |
| The browser extension | Someone clicks a toolbar button or an automation on a Plex screen. | Building automations |
| An Application Designer app | A button, form or screen in an app runs it. | Application Designer |
| Excel and Power BI | A workbook or report refreshes its data. | Scripts in Excel & Power BI |
| The Hosted File Gateway | A partner uploads a file to your SFTP or FTPS address. | The gateway script handler |
| Inbound email | Mail arrives at a hosted address from an allowed sender. | Inbound email |
| Another script | A script calls it with scripts.run or scripts.runAsync. | Writing scripts |
| A workflow | A workflow's Run Script / Workflow step calls it. | Workflows and JavaScript |
| Hooks | Before or after a carrier label, a DataMagik Mobile transaction, or a Shop Floor write to Plex, to stop it, change it or follow it up. | Shipping hooks, Mobile transaction hooks, Shop Floor transaction hooks |
| AI assistants | An assistant calls the script as a tool. Turn this on in the editor under Script settings › Assistant Tool with Expose as assistant tool. | Writing scripts |
Scripts can also be started by dashboards loading their data, by an approval being decided, by files arriving on an FTP or file-system connector, and by EDI documents. The editor's Context browser › Triggers tab lists every kind of start and the context each one sends.
Where to go next
- Writing scripts: the editor,
context, return values, testing, versions, settings and limits. - Script API reference: everything a script can call.
- Script executions: finding and reading runs.
- Script schedules and Script webhooks.
- Integrations: watching a group of scripts as one.
- Script credentials: keeping passwords and keys out of your code.
- Plex API access.
- Scripts in Excel & Power BI.
- Workflows: the same automations, built without code.