Namespace and key cannot be changed for existing record. Try creating a new one

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

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,

1 Like

I have tried to exclude the namespace and key fields. The features are running well now. Thank you very much, Liam!

Great to hear!