discountCodeBasicCreate API for specific Customer Error Error "Invalid global id"

cyotani
Visitor
2 0 0

I'm trying to use the GQL to create at discount code for a specific cusotmer. I was succesful in using the provided template code in the link below to add a discount code for all customers. But when I modify it I get a  "Invalid global id" Error.  

 

Documentation:

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/discountCodeBasicCreate

 

Here are my variables

cyotani_0-1715790898386.png

 

 

This is the customer id I'm using to test.

cyotani_1-1715788618040.png

 

I'm using the provided template mutation code from the documentation:

 

 

query = '''
mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
  discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
    codeDiscountNode {
      codeDiscount {
        ... on DiscountCodeBasic {
          title
          codes(first: 10) {
            nodes {
              code
            }
          }
          startsAt
          endsAt
          customerSelection {
            ... on DiscountCustomerAll {
              allCustomers
            }
          }
          customerGets {
            value {
              ... on DiscountPercentage {
                percentage
              }
            }
            items {
              ... on AllDiscountItems {
                allItems
              }
            }
          }
          appliesOncePerCustomer
        }
      }
    }
    userErrors {
      field
      code
      message
    }
  }
}
'''

 

 

 

Error response:

 

 

 

{"errors":[{"message":"Variable $basicCodeDiscount of type DiscountCodeBasicInput! was provided invalid value for customerSelection.customers.add.0 (Invalid global id '7929561841982')","locations":[{"line":2,"column":34}],"extensions":{"value":{"title":"test3","code":"test3","startsAt":"2022-06-21T00:00:00Z","endsAt":"2022-09-21T00:00:00Z","customerSelection":{"all":false,"customers":{"add":[7929561841982]}},"customerGets":{"value":{"percentage":0.2},"items":{"all":true}},"appliesOncePerCustomer":true},"problems":[{"path":["customerSelection","customers","add",0],"explanation":"Invalid global id '7929561841982'","message":"Invalid global id '7929561841982'"}]}}]}

 

 

 

 

 

 

Reply 1 (1)

cyotani
Visitor
2 0 0

I finally got it to work. The fix was that the customer input was not correct it should be formated like this:

 

 

"customerSelection": {
    'customers': {
        'add': ['gid://shopify/Customer/7936674496830']
    }
},

 

 

Also, if you want the customer returned in the response you need to modify the mutation like this:

 

 

          customerSelection {
            ... on DiscountCustomerAll {
              allCustomers
            }
            ... on DiscountCustomers {
              customers  { 
              id
               firstName
               lastName
               email
               }
            }
          }