Update product metafield with metaobject entry through Shopify flow

This was a tough one, but I got it solved. I used the “Update product metafield” action. So for anyone else who wants to know how to use flow to update the value of the material metafield, or any other metafield that contains a list of metaobjects:

RUN CODE OUTPUT

In the Write Code, I’m generating a set containing all the GIDs that for the materials which that product has. Then I’m converting the set to a string like this:

const outputData = JSON.stringify(Array.from(processingData)); //For array output, convert Set to JSON string

Define Inputs:

The input query allows you to pull data from

the step’s environment. The resulting object will be

available in your script as the input argument.

query {
  product {
    tags
    material {
      value {
        label
        system {
         id
        }
      }
  }
}
}

Define Outputs:

type Output {
“The value for the Category”
outputData: String!
}

LOG OUTPUT

Value: {{runCode.outputData}}

Example result when viewing a workflow run:

["gid://shopify/Metaobject/70110609636","gid://shopify/Metaobject/71006978276","gid://shopify/Metaobject/70756073700"]

ACTION: Update product metafield

Value: {{runCode.outputData}}

Example “Input” In the step data when viewing a workflow run:

{
"product_id": "gid://shopify/Product/8528988766436",
"metafield": {
"namespace": "shopify",
"key": "material",
"type": "list.metaobject_reference"
},

“value”: “["gid://shopify/Metaobject/70110609636","gid://shopify/Metaobject/71006978276","gid://shopify/Metaobject/70756073700"]”
}