How to update customers metafields

Solved
dev-karlmax
New Member
5 0 0

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?


Accepted Solution (1)

Accepted Solutions
danloomer
Shopify Staff
6 3 2

This is an accepted solution.

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.

View solution in original post

Replies 4 (4)
danloomer
Shopify Staff
6 3 2

This is an accepted solution.

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.

View solution in original post

dev-karlmax
New Member
5 0 0

Hi @DA 

customerUpdate with the id works for me

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

danloomer
Shopify Staff
6 3 2

Glad adding the ID worked!

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

dev-karlmax
New Member
5 0 0

It was 2021-07, no it works 

 

Thanks a lot, Ralph