Discussing APIs and development related to customers, discounts, and order management.
I'm implementing a Discount update function using discountCodeAppUpdate, and meet this problem. When I call to update a discount, I received this message in the response body "Namespace and key cannot be changed for an existing record. Try creating a new one.". Is there anyone who meets this problem and how to solve it?
Here is my query:
query = <<~QUERY mutation discountCodeAppUpdate($codeAppDiscount: DiscountCodeAppInput!, $id: ID!) { discountCodeAppUpdate(codeAppDiscount: $codeAppDiscount, id: $id) { codeAppDiscount { discountId title endsAt } userErrors { field message } } } QUERY
Here are my params:
{ :id=>"gid://shopify/DiscountCodeNode/xxxxxxxxxxxxx", :codeAppDiscount=>{ :code=>"123", :title=>"Test", :appliesOncePerCustomer=> false, :metafields=> { "namespace"=>"$app:discount-code-advanced", "key"=>"function-configuration", "type"=>"json", "value"=>"{\"allOrAnyCriteria\":\"all\",\"selectCustomersBy\":{\"name\":\"none\",\"inputs\":[]},\"selectCartBy\":{\"name\":\"none\",\"inputs\":[]},\"selectLineItemBy\":{\"name\":\"none\",\"inputs\":[]},\"discount\":\"{{code => \\\"123\\\", type => \\\"fixed\\\", amount => \\\"123\\\"}}\"}", "id"=>"gid://shopify/Metafield/xxxxxxxxxxxx" } } }
Here is the response body:
{"data"=>{"discountCodeAppUpdate"=>{"codeAppDiscount"=>nil, "userErrors"=>[{"field"=>["codeAppDiscount", "metafields", "0", "id"], "message"=>"Namespace and key cannot be changed for existing record. Try creating a new one."}]}}, "extensions"=>{"cost"=>{"requestedQueryCost"=>10, "actualQueryCost"=>10, "throttleStatus"=>{"maximumAvailable"=>1000.0, "currentlyAvailable"=>990, "restoreRate"=>50.0}}}}
Solved! Go to the solution
This is an accepted solution.
Hi JohnDoe3,
What might be happening here is that the API may be thinking that you're trying to update the namespace and key of a metafield (which is not possible), rather than the value of the metafield. If you exclude the namespace and key fields from the metafields input are you still seeing an error?
Hope this helps,
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
This is an accepted solution.
Hi JohnDoe3,
What might be happening here is that the API may be thinking that you're trying to update the namespace and key of a metafield (which is not possible), rather than the value of the metafield. If you exclude the namespace and key fields from the metafields input are you still seeing an error?
Hope this helps,
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
I have tried to exclude the namespace and key fields. The features are running well now. Thank you very much, Liam!
Great to hear!
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog