Bulk trigger Shopify Flow with historical data, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Bulk trigger Shopify Flow with historical data

Do you wish Shopify Flow workflows could run on historical data? Now they can! :D

Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes start date, end date, and flag value.

15-day free trial – unlimited tasks

Documentation

Do you wish Shopify Flow workflows could run on historical data? Now they can! :D

This task queries your orders, and calls Shopify Flow for each order returned. You can optionally provide a date range to limit the orders to be processed. If provided, these dates must be in this format YYYY-MM-DD.

It includes a flag value, intended to help Flow workflows identify the Mechanic order triggers that are relevant, ignoring those that are not. The flag value only has significance if there are Flow workflows looking for its value, using workflow conditions. A Flow condition for this purpose might read like, if User string is equal to 'mechanic-import'.

This task is written to process orders, but can be modified to process products or customers. To make a change like this, open the task code, and update the bulk operation to query for the right resources. Then, update the Flow action to send product_id or customer_id as appropriate, instead of order_id.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
start date, end date, flag value
Code
{% assign start_date = options.start_date %}
{% assign end_date = options.end_date %}

{% if start_date %}
  {% assign valid_start_date = start_date | parse_date: "%Y-%m-%d" %}
  {% if valid_start_date == blank %}
    {% error message: "A start date could not be parsed. If you choose to include, ensure the format is YYYY-MM-DD."  %}
  {% endif %}
{% endif %}

{% if end_date %}
  {% assign valid_end_date = end_date | parse_date: "%Y-%m-%d" %}
  {% if valid_end_date == blank %}
    {% error message: "An end date could not be parsed. If you choose to include, ensure the format is YYYY-MM-DD."  %}
  {% endif %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders
      {% if start_date and end_date %}
        (query: "created_at:>={{start_date}} AND created_at:<{{end_date}}")     
      {% elsif start_date %}
        (query: "created_at:>={{start_date}}")
      {% elsif end_date %}
        (query: "created_at:<{{end_date}}")    
      {% endif %}  
       {
        edges {
          node {
            __typename
            legacyResourceId 
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture bulkOperation_objects_jsonl %}
      {"__typename":"Order","legacyResourceId":"1234567890"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = bulkOperation_objects_jsonl | parse_jsonl %}
  {% endif %}

  {% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
{% endif %}

{% for order in orders %}
  {% action "flow" %}
    {
      {% comment %}
        Remember, Flow only accepts numeric IDs - not full global ID strings.
      {% endcomment %}
      "order_id": {{ order.legacyResourceId }},

      {% comment %}
        This flag value is intended to be used in Flow conditions; see the task docs for more details.
      {% endcomment %}
      "user_string": {{ options.flag_value | append: "" | json }}
    }
  {% endaction %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Flag value
mechanic-batch