Solved

B2B API companyAssignCustomerAsContact issue

BojanAnchev
Tourist
4 1 1

I'm having trouble executing this API GraphQL mutation: companyAssignCustomerAsContact

I have a company and customer created but I cannot create/assign that customer as a contact to that company I was following the documentation and tried implementing it with Python.

import json
import requests

def assign_customer_as_contact(url, access_token, companyId, customerId):
# Define the GraphQL mutation
mutation = """
mutation {
companyAssignCustomerAsContact(
customerId: "gid://shopify/Product/your_contact_id"
companyId: "gid://shopify/Product/your_company_id"
) {
userErrors {
field
message
}
}
}
"""

# Prepare the variables for the mutation
mutation = mutation.replace('your_contact_id', customerId)
mutation = mutation.replace('your_company_id', companyId)

print(mutation)
# Setup headers for the request
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": access_token,
}

# Setup the request payload
payload = {"query": mutation}

# Send the request to the Shopify GraphQL API
response = requests.post(url, headers=headers, json=payload)

# Handle the response
if response.status_code == 200:
data = response.json()
if "errors" in data:
print("Error:", data["errors"])
else:
print("Customer assigned as contact successfully.")
print(json.dumps(data["data"], indent=2))
else:
print("Failed to assign customer as contact.", response.text)

if __name__ == "__main__":
api_url = ''
api_access_token = ''

company_id = "3131"
customer_id = "3131"

# Call the function to assign customer as contact
assign_customer_as_contact(api_url, api_access_token, company_id, customer_id)

I got the following error:

Error: [{'message': 'invalid id', 'locations': [{'line': 3, 'column': 3}], 'path': ['companyAssignCustomerAsContact']}]

What am I doing wrong I assume they are referring to these customers.

Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1312 215 454

This is an accepted solution.

Hey @BojanAnchev,

 

I see your script is replacing the legacy resource ID, the number at the end of the Shopify Global ID value, but both arguments to the mutation are set as Products rather than a Customer and a Company

 

companyAssignCustomerAsContact(
    customerId: "gid://shopify/Product/your_contact_id"
    companyId: "gid://shopify/Product/your_company_id"
  ) 

 

Looks like our documentation for that mutation has Products listed as placeholder variables as well, so I've already sent some feedback to request the docs be updated. 

 

All the best!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

View solution in original post

Replies 2 (2)

ShopifyDevSup
Shopify Staff
1312 215 454

This is an accepted solution.

Hey @BojanAnchev,

 

I see your script is replacing the legacy resource ID, the number at the end of the Shopify Global ID value, but both arguments to the mutation are set as Products rather than a Customer and a Company

 

companyAssignCustomerAsContact(
    customerId: "gid://shopify/Product/your_contact_id"
    companyId: "gid://shopify/Product/your_company_id"
  ) 

 

Looks like our documentation for that mutation has Products listed as placeholder variables as well, so I've already sent some feedback to request the docs be updated. 

 

All the best!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

BojanAnchev
Tourist
4 1 1

@ShopifyDevSup thank you, this resolved the issue.

def assign_company_contact(shopify_store, company_id, customer_id):
graphql_url = get_graphql_endpoint(shopify_store)
access_token = shopify_store.shopify_api_password
variables = {
"companyId": "gid://shopify/Company/%s" % company_id,
"customerId": "gid://shopify/Customer/%s" % customer_id
}
graphql_mutation = """
mutation companyAssignCustomerAsContact($companyId: ID!, $customerId: ID!) {
companyAssignCustomerAsContact(companyId: $companyId, customerId: $customerId) {
companyContact {
id
# CompanyContact fields
}
userErrors {
field
message
}
}
}
"""
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": access_token,
}

try:
response = requests.post(
graphql_url,
json={"query": graphql_mutation, "variables": variables},
headers=headers
)
response = response.json()
logging.info('-- ASSIGN COMPANY CUSTOMER --')
logging.info(response)
except Exception as e:
logging.info("Error linking customer and company:")
logging.info(e)