Update product metafield with metaobject entry through Shopify flow

I’m hoping to use flow to update a metaobject reference metafield when new products are added through Shopify flow. Is this possible?

Doesn’t seem to be a clear way to select a specific metaobject entry.

Hi ECommBadassery,

This article may help

How to Update Product Metafield with MetaObject Entry Through Shopify Flow

Good luck

Tony

This article appears to be “written” by AI and is incorrect.

2 Likes

Yes, that should be possible. I can’t really describe how to do it because it depends on which resource it’s access on and exactly what you want to update. If it you change it to a different metaobject, that’s possible. If you want to change the Metabject itself, that is not really updating a metafield but the metaobject. It should be possible, but you would need to use Send HTTP Request currently.

@paul_n Thanks for the heads up on the article, just looked at it now and agree.

To clarify the use case…

I have a meta object for details about what’s included in the product. The entries are specific to different product types.

Then I have a product metafield that references that metaobject

For product type A - I’d like to select Entry A

For product type B - I’d like to select Entry B

For product type C - I’d like to select Entry C

Is this a small set of products or a lot of products?

@paul_n It’s a pretty small set overall and only need workflow for when she adds new products

Since this is a small set of them, you can hard-code the entries into the metafield. I think you would use the Update product metafield action, and choose “Metaobject” as the type. In the value field, you would enter something like: gid://shopify/Metaobject/123. Be careful to avoid newlines/spaces/etc. The ID for your metaobject can be found in the URL of the entry (just replace that 123 part). Make sure you enter the right namespace and key as well.

1 Like

Woot woot, worked like a charm! Thanks @paul_n

What’s the exact format for Value to get this to work? I’m Trying to update the material meta field which is a list of meta objects. I’ve tried this for the value but it gives an error. ‘gid://shopify/TaxonomyValue/1637’:

{%- for tags_item in product.tags -%}
{%- if tags_item contains “Aluminum” -%}
gid://shopify/TaxonomyValue/1637
{%- endif -%}
{%- endfor -%}

The error message is:

Exception: Got error updating metafield: “Value is invalid JSON: unexpected token at ‘gid://shopify/TaxonomyValue/1637’.” For value: “gid://shopify/TaxonomyValue/1637”, Type: list.metaobject_reference

I also tried ["gid://shopify/TaxonomyValue/1637"] and got the error “Exception: Got error updating metafield: “Value require that you select a metaobject.” For value: “[“gid://shopify/TaxonomyValue/1637”]”, Type: list.metaobject_reference”.

That is a list of metaobjects. So it’s like [“gid://shopify/Metaobject/123”,“gid://shopify/Metaobject/123”]

Probably what you actually need to do then is update a Metaobject. To do that you would need to call MetaobjectUpsert via Send Admin API request. Here’s an example request that works:

{
  "handle": {
    "type": "recipe",
    "handle": "scrambled-eggs"
  },
  "metaobject": {
    "handle": "scrambled-eggs",
    "fields": [
      {
        "key": "ingredients",
        "value": "[\"eggs\",\"milk\",\"foo\"]"
      }
    ]
  }
}

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"]”
}

1 Like

Your solution helped me a lot. I had never used the Run Code element in Shopify Flows. For anyone else with this issue, these are very helpful: https://github.com/Shopify/flow-code-examples/tree/main/run-code-examples