Update product metafield with metaobject entry through Shopify flow

Solved

Update product metafield with metaobject entry through Shopify flow

eCommBadassery
Shopify Partner
5 0 0

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. 

Jessica
eCommerce & Email Marketing Strategist
eCommerce Badassery
Accepted Solution (1)
paul_n
Shopify Staff
1766 194 412

This is an accepted solution.

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.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

View solution in original post

Replies 11 (11)

Tony-BEMEApps
Shopify Partner
16 1 7

Hi ECommBadassery,

This article may help 

How to Update Product Metafield with MetaObject Entry Through Shopify Flow

Good luck

Tony

BeMEApps
SEO Apps and Community
paul_n
Shopify Staff
1766 194 412

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

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

paul_n
Shopify Staff
1766 194 412

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 | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
eCommBadassery
Shopify Partner
5 0 0

@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

 

 

 

Jessica
eCommerce & Email Marketing Strategist
eCommerce Badassery
paul_n
Shopify Staff
1766 194 412

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

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
eCommBadassery
Shopify Partner
5 0 0

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

Jessica
eCommerce & Email Marketing Strategist
eCommerce Badassery
paul_n
Shopify Staff
1766 194 412

This is an accepted solution.

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.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
eCommBadassery
Shopify Partner
5 0 0

Woot woot, worked like a charm! Thanks @paul_n 

Jessica
eCommerce & Email Marketing Strategist
eCommerce Badassery
ri31
Shopify Partner
38 2 13

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

ri31_0-1745928212756.png

 

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".

 

If my post is helpful, hit Like to help others find a solution.
paul_n
Shopify Staff
1766 194 412

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\"]"
      }
    ]
  }
}
Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
ri31
Shopify Partner
38 2 13

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:

 

ri31_0-1746014513175.png

 

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}}

ri31_1-1746014643825.png

 

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

ri31_2-1746014769054.png

 

 

 

If my post is helpful, hit Like to help others find a solution.