How to update customers metafields

Hello,

I need to add some extra info about a customer. So my plan is to use the metafields of a customer object to store these extra values. I’m able to add new metafields, but when I try to update existing ones, I get an error message: “Key must be unique within this namespace on this resource”.

To update the metafields, I use this GraphQL admin mutation: https://shopify.dev/api/admin-graphql/2021-10/mutations/customerUpdate

This is the request body:

mutation {
  customerUpdate(input: {
    id: "gid://shopify/Customer/xxx", 
    metafields: [
      {key: "foo", value: "bar", type: "single_line_text_field", namespace: "test"}, 
      {key: "foo2", value: "bar2", type: "single_line_text_field", namespace: "test"}
    ]
  }) {
    userErrors {
      field
      message
    }
    customer {
      metafields(first: 2, namespace:"test") {
        edges {
          node {
            key
            value
          }
        }
      }
    }
  }
}

And here is the result:

{
    "data": {
        "customerUpdate": {
            "userErrors": [
                {
                    "field": [
                        "metafields",
                        "7",
                        "key"
                    ],
                    "message": "Key must be unique within this namespace on this resource"
                },
                {
                    "field": [
                        "metafields",
                        "8",
                        "key"
                    ],
                    "message": "Key must be unique within this namespace on this resource"
                }
            ],
            "customer": {
                "metafields": {
                    "edges": [
                        {
                            "node": {
                                "key": "foo",
                                "value": "bar"
                            }
                        },
                        {
                            "node": {
                                "key": "foo2",
                                "value": "bar2"
                            }
                        }
                    ]
                }
            }
        }
    }
}

Is there a way to update a customer’s metafields?

Hi @dev-karlmax !

You’re getting that error because you aren’t passing in the existing metafield’s ID to the customerUpdate mutation. You can grab the ID after creation and then pass it along to subsequent updates.

Alternatively you could use the metafieldsSet mutation which doesn’t require passing the ID on updates.

1 Like

Hi @Anonymous

customerUpdate with the id works for me :slightly_smiling_face:

But when I use the metafieldsSet mutation, I get this error: “Field ‘metafieldsSet’ doesn’t exist on type ‘Mutation’”.

mutation {
  metafieldsSet(metafields: [
    {namespace: "xxx", ownerId: "gid://shopify/Customer/xxx", type: "single_line_text_field", key: "university", value: "foo"}, 
    {namespace: "xxx", ownerId: "gid://shopify/Customer/xxx", type: "single_line_text_field", key: "faculty", value: "bar"}, 
    {namespace: "xxx", ownerId: "gid://shopify/Customer/xxx", type: "single_line_text_field", key: "level", value: "foobar"}
  ]) {
    metafields {
      key
      value
    }
    userErrors {
      field
      message
    }
  }
}

Thank you for your help :slightly_smiling_face:

1 Like

Glad adding the ID worked!

What API version are you using? The metafieldsSet mutation is only available in 2021-10 and up.

It was 2021-07, no it works

Thanks a lot, Ralph