Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Filling in a metafield on customerUpdate (Python)

Solved

Filling in a metafield on customerUpdate (Python)

GregJones55
Shopify Partner
14 4 2

I want to fill in an empty metafield on customers but can't seem to figure out the appropriate syntax for the GraphQL call. This is where I'm at:

 

"""
mutation {
    customerUpdate(input:{
                id:"%s",
                metafields: {
                namespace:"custom",
                key:"value",
                type: "single_line_text_field",
                value:"%s",}
                }){
        customer {
            id
            firstName
            
            }
        userErrors{
            field
            message
            }    
        }
}
"""%(custID, value)

This works for me with the metafield id to update a metafield if one already exists, but it does nothing if it's empty and doesn't even error code anything. The sources I've found online have said this should essentially work. Any insights would be appreciated.

Accepted Solution (1)

GregJones55
Shopify Partner
14 4 2

This is an accepted solution.

I figured out the problem. The namespace and the key needed to be the same as was originally set for the metafield originally but had to be separated. For example, let's say when I made the metafield it said "namespacecustom.keyvalue" then namespace needed to be "namespacecustom" and key needs to be "keyvalue". I kept trying a lot of other things but I guess never tried it this way until hours later. 

 

This doesn't matter when you're updating it because the id takes care of everything, but when you're 'creating' a new metafield (i.e. filling in an 'empty' one), it's very important to get right or nothing happens.

View solution in original post

Reply 1 (1)

GregJones55
Shopify Partner
14 4 2

This is an accepted solution.

I figured out the problem. The namespace and the key needed to be the same as was originally set for the metafield originally but had to be separated. For example, let's say when I made the metafield it said "namespacecustom.keyvalue" then namespace needed to be "namespacecustom" and key needs to be "keyvalue". I kept trying a lot of other things but I guess never tried it this way until hours later. 

 

This doesn't matter when you're updating it because the id takes care of everything, but when you're 'creating' a new metafield (i.e. filling in an 'empty' one), it's very important to get right or nothing happens.