DataMagik Script Engine

Updated Nov 9, 2026
DataMagik Automate

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 itemWhat it is for
ScriptsThe script editor: find, write, test, save and configure scripts. See Writing scripts.
Data for ExcelEvery script your company has published to Excel, with its link. See Scripts in Excel & Power BI.
SQL EditorWrite and save the Plex SQL queries that scripts run with odbc.executeQuery. See SQL Editor.
Plex Data SourcesBrowse the Plex data sources that scripts call with plex.execute.
CredentialsPasswords, API keys and tokens that scripts use, stored once for the whole company. See Script credentials.
GitYour own Git access, for keeping scripts in a repository. See Git integration.
SchedulesRun scripts by the clock. See Script schedules.
ExecutionsEvery run in your company: what ran, what started it, how it ended, what it logged and what it returned. See Script executions.
IntegrationsGroups of scripts that work together, such as one partner's integration, watched as one: their health, traffic and failures. See Integrations.
WorkflowsThe visual builder. See Workflows.
Inbound EmailHosted email addresses that run a script when mail arrives. See Inbound email.
Email TemplatesSaved 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

  1. Open Script Engine › Scripts.
  2. Click the + button (New Script) in the toolbar. A script named New Script is created with some starter code and opens in the editor.
  3. Click the Script settings button, change Script Name to something that says what it does, add a Description, and click Save Changes.
  4. 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 }
      };
    }
  5. Click the Context browser button and, on the Test input tab, type the input for the test: { "part_no": "44-9912" }.
  6. 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 what main returned.
  7. 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 byWhenRead more
Test, Debug and Run in the editorYou press the button. Test and Debug run the code in the editor; Run runs the saved version.Writing scripts
A scheduleOn a timetable, such as 07:00 every weekday.Script schedules
A webhook or API callAnother 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 extensionSomeone clicks a toolbar button or an automation on a Plex screen.Building automations
An Application Designer appA button, form or screen in an app runs it.Application Designer
Excel and Power BIA workbook or report refreshes its data.Scripts in Excel & Power BI
The Hosted File GatewayA partner uploads a file to your SFTP or FTPS address.The gateway script handler
Inbound emailMail arrives at a hosted address from an allowed sender.Inbound email
Another scriptA script calls it with scripts.run or scripts.runAsync.Writing scripts
A workflowA workflow's Run Script / Workflow step calls it.Workflows and JavaScript
HooksBefore 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 assistantsAn 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

Was this page helpful?