Powered By GitBook
How can I update existing metafields?
Metafields may be managed using the Shopify action, whether you use REST or GraphQL. While metafields can be uniquely referenced for a resource, using their namespace and key, any updates to them must include the metafield ID.
Any attempts to update an existing metafield, without providing the ID, will receive this error from Shopify:
1
Received a Shopify error (resource invalid): metafields.key must be unique within this namespace on this resource
Copied!
Use the following code as an example for how you might create a metafield if is missing, or update it if it already exists, using either REST or GraphQL:
1
{% assign customer = shop.customers[1234567890] %}
2
​
3
{% assign rest_metafield = customer.metafields | where: "namespace", "testing" | where: "key", "REST" | first %}
4
{% assign graphql_metafield = customer.metafields | where: "namespace", "testing" | where: "key", "GraphQL" | first %}
5
​
6
{% action "shopify" %}
7
[
8
"update",
9
[
10
"customer",
11
1234567890
12
],
13
{
14
"metafields": [
15
{
16
"id": {{ rest_metafield.id | json }},
17
"namespace": "testing",
18
"key": "REST",
19
"value": "test!",
20
"value_type": "string"
21
}
22
]
23
}
24
]
25
{% endaction %}
26
​
27
{% action "shopify" %}
28
mutation {
29
customerUpdate(
30
input: {
31
id: "gid://shopify/Customer/1234567890"
32
metafields: [
33
{
34
id: {{ graphql_metafield.admin_graphql_api_id | json }}
35
namespace: "testing"
36
key: "GraphQL"
37
value: "test!"
38
valueType: STRING
39
}
40
]
41
}
42
) {
43
userErrors {
44
field
45
message
46
}
47
}
48
}
49
{% endaction %}
Copied!
Last modified 6mo ago
Copy link