Mechanic Get Mechanic
Task library

Bulk trigger Shopify Flow with historical data

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

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

  • Start date
  • End date
  • Flag value

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work

Task 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 %}
Mechanic Liquid reference

Default option values

Flag value
mechanic-batch