Run code

The Run code action executes JavaScript. You can pass data from previous steps in the workflow as input to the Run code action, and return values to be used in subsequent steps.

Fields

The Run code action contains the following fields.

Fields used in the Run code action.
FieldDescription
InputRequired. A GraphQL query to use data from previous steps as input to the Run code action.
OutputRequired. A representation of the data to be returned by the action, defined in GraphQL's schema definition language (SDL).
CodeRequired. The JavaScript that the Run code action will execute.

Input data

Input data can be passed from steps that happen before the Run code action. To include this data, you can write a GraphQL query in the Input field. The data returned from the query will be available as the input argument to the function marked with export default, called main by convention.

The input query is a query for Flow environment data not a query to the Shopify Admin API. Therefore you can't insert a Shopify query in the input data. Additionally, Flow handles the edges and nodes in the query, so you don't need to add those or other paging syntax to your query.

An example input that gets an order note and the title of a line item:

{
  order {
    note
    lineItems {
      title
    }
  }
}

This data is converted to an input variable that can be used in the code:

export default function main(input) {
  // input.order.note
  // input.order.lineItems[0].title
}

Inputs can also be destructured in the function signature:

export default function main({order}) {
  // order.note
  // order.lineItems[0].title
}

Metafields

To access single metafield values in Run code, you must first add the metafield to Flow's environment data. This be done in any action or condition other than Run code. For example, you can add a Log output action and then follow the instructions to add the metafield.

After a metafield has been added to the workflow environment, you can access it in Run code. For example, suppose that you added an Order metafield with the alias name of giftMessage. You can then access it in your input query:

{
  order {
    giftMessage {
      value
    }
  }
}

Output data

The Run code action can return custom data. To define the type of the data that the code will return, use the Output field and GraphQL's Schema definition language (SDL). Supported output types are String, Int, Float, Boolean, and ID, which follow the SDL specification for how to specify required fields, lists, and custom data.

For example, to return a string called giftMessage and a number called totalGifts:

type Output {
  "The message to include in the gift"
  giftMessage: String!
  "The total number of gifts"
  totalGifts: Int!
}

The comments are optional, but will be used to describe the data in the Flow UI. To output this data in the JavaScript code, return an object matching the type:

export default function main(input) {
  // your code
  return {
    giftMessage: 'Hello',
    totalGifts: 1,
  };
}

You can also define a custom type to return more complex data. For example, to return a type called Gift that contains a string called message and a number called amount:

type Output {
  "The gift to send"
  gifts: [Gift!]!
}

type Gift {
  "The message to include in the gift"
  message: String!
  "The total number of gifts"
  amount: Int!
}

To access this data in steps that follow this action, use the variable named Run code, which will be typed according to the Output schema you define in the Run code action configuration. You can use this variable in both conditions and actions.

Console.log

You can use console.log to output data to Flow's workflow run log for troubleshooting purposes. The output will be visible in the Run log for the workflow. For example the following are valid:

export default function main(input) {
  console.log('Hello, world!');
  //Hello, world!
  console.log(input);
  // { order: { note: 'Hello', lineItems: [ { title: 'World' } ] } }
  console.log(input.order, 'is the order');
  // { note: 'Hello', lineItems: [ { title: 'World' } ] }
  // is the order

  return {
    giftMessage: 'Hello',
    totalGifts: 1,
  };
}

You cannot use console.info, console.error, or other functions in the Run code action.

Example

Examples for the code action can be found in the Flow examples repository.

Limitations

The Run code action has the following limitations:

  • The Run code action supports ECMA2020 JavaScript. It does not support the NodeJS or CommonJS APIs, or importing modules.
  • Your code cannot make http calls (fetch).
  • Random and clock-based functions cannot be used. Date data, such as a scheduledAt or createdAt can be passed in as an input.
  • Console.log does not log to the browser console.
  • You cannot add metafield alias directly in the Run code action. View the metafields section for how to accomplish this.

In addition, the following limits are enforced:

  • Input data query is limited to 5000 characters.
  • Output data schema is limited to 5000 characters.
  • Output data payload and Console.log output are limited to a combined 50kb.
  • Code cannot be longer than 50000 characters.
  • Total execution duration is limited to 5 seconds.
  • Memory usage is limited to 10MB.

Templates

Add free (discounted 100%) item to new orders

This template adds a free, discounted item to new orders if the item is in stock. It checks inventory, applies a 100% discount, and updates the order without notifying the customer. This automation would be useful to (1) enhance customer satisfaction by including a surprise free item in their order, (2) promote new or less-known products by adding them as free samples in orders, or (3) to streamline promotions by automating the inclusion and discounting of free items. View template

Cancel inactive returns

Automatically cancel a return that has not been refunded or restocked within a certain period of time. View template

Convert tags with a prefix to a product metafield using the Run code action

This workflow will take tags that start with a prefix such as 'color:' and add them to a product metafield list. It uses the Run code action to parse the tags and existing items in the metafield list. The workflow runs when a product is created, but can be manually run on existing products. View template

Send notification when a customer orders multiple variants of a product

Receive an email notification when a customer orders multiple variants of the same product. View template

Send notification when order contains a bundle

This workflow begins when an order is created and checks if any of the products in the order were purchased as part of a bundle. If found an internal email is sent with the bundle details. Additionally you can use this workflow as a starting point for any use case that needs to detect the presence of a bundle within an order. View template

Tag orders with associated UTM campaign

Adds tags to orders that contain the names of any associated UTM campaigns. View template

Can’t find the answers you’re looking for? We’re here to help.