Custom Code
Last updated
Last updated
The Custom Code tool allows you to add complex logic to a workflow that might otherwise be difficult with MESA's built-in tools. You can create custom integrations to third-party services, even those that MESA does not yet integrate, and code custom business logic using JavaScript.
Click the </> Edit Code button to edit your custom code.
Code is executed in a V8 environment and can be written in standard JavaScript or ES6. When this step is run, the script()
method will be called with two parameters: prevResponse
and context
. In order for your workflow to continue its subsequent steps, be sure to include a call to Mesa.output.next(response);
somewhere within your script()
method.
context
objectsteps
Object
An object containing the variables for each step that has been run so far, named with the step's unique key.
For example, to access a step with a key of "shopify" use: context.steps['shopify']
.
Trigger
objectmetadata
Object
The values entered in configuration fields, with variable replacements.
raw_metadata
Object
The values entered in configuration fields, without variable replacements.
fields
Object
An object of the configuration fields from the UI.
Task
objectcontext.headers
Object
A key-value object of the HTTP headers passed to this task.
is_test
Object
Is the current task being run as a test?
Libraries offer built-in functionality that extends the power of your code. The MESA SDK library is included by default and other libraries can be imported as needed.
To debug your script, it can be helpful to log data at a particular step in your code. This can be done by calling Mesa.log.info()
. This is equivalent to console.log()
in browser-based JavaScript. When the workflow runs, the info will be available in your workflow's Logs tab.
Throw a JavaScript error to halt the execution of your workflow:
In the User Interface, this will look like:
Use Mesa.request.get()
to make HTTP requests to external APIs. This is the equivalent to fetch()
in browser-based JavaScript.
All system timezones use your Shopify Store's timezone. You can view and update your timezone from the Shopify Dashboard: Admin > Settings > General, under "Standards and formats". If you need to get dates in a different timezone, use the Mesa.date.setTimezone(timezone) method.
Scripts are run in a stock V8 environment. This means that some common NodeJS methods are not available:
request()
: Alternative: Mesa.request.*()
Documentation.
console.log()
: Alternative: Mesa.log.*()
Documentation.
DOM manipulation methods: Alternative: Mesa.xml.decode()
Documentation.