Skip to content

Custom Code

Write JavaScript in the MESA Custom Code tool to add complex logic, build integrations with third-party services and handle cases built-in tools cannot.

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.

Screenshot of the MESA Custom Code tool. Spotlight the Edit Code button.
const Mesa = require('vendor/Mesa.js');
/**
* A MESA Script exports a class with a script() method.
*/
module.exports = new class {
/**
* MESA Script
*
* @param {object} prevResponse The response from the previous step
* @param {object} context Additional context about this task
*/
script = (prevResponse, context) => {
// Retrieve the Variables Available to this step
const vars = context.steps;
// Add your custom code here
let response = {}
// Call the next step in this workflow
// response will be the Variables Available from this step
Mesa.output.next(response);
}
}

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.

PropertyTypeDescription
stepsObject

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'].

triggerTriggerThis object is the configuration of the current step being run.
taskTaskThis object is information about the current task's execution.
PropertyTypeDescription
metadataObjectThe values entered in configuration fields, with variable replacements.
raw_metadataObjectThe values entered in configuration fields, without variable replacements.
fieldsObjectAn object of the configuration fields from the UI.
PropertyTypeDescription
context.headersObjectA key-value object of the HTTP headers passed to this task.
is_testObjectIs 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.

Screenshot of a MESA workflow Logs tab showing logged output from Mesa.log.info in a Custom Code step. Spotlight the logged info entries.

Throw a JavaScript error to halt the execution of your workflow:

throw new Error("your error description")

In the User Interface, this will look like:

Screenshot of the MESA workflow builder showing a thrown JavaScript error from a Custom Code step in the user interface. Spotlight the error message.

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.
  • async or await: All Mesa.request.*() methods are synchronous, so async and await are not necessary.
  • DOM manipulation methods: Alternative: Mesa.xml.decode() Documentation.

In this section