Historic orders linking to Company / Customer

hi

I am migrating a site to Plus.

I have successfully imported 100K orders.
I would like to link the orders to Companies (Which I am also migrating). This is important to the customer as do B2B.

I understand that this can be done in admin which appears to call the CompanyLocationMigrateOrdersMutation but this does not appear to be available in the API.
CompanyLocationRevertMigratedOrders is also needed in case I need to reverse.

Is there a way I can get access to these API’s or another process?

Any help appreciated
Thanks
Mark

1 Like

Hey Mark,

Good catch finding those mutations in the admin UI. Unfortunately, CompanyLocationMigrateOrdersMutation and CompanyLocationRevertMigratedOrders are internal-only and not exposed in the public API. I confirmed this with our B2B product team.

The current API approach for linking orders to companies only works during order creation via the REST API. You’d include both customer.id and company.location_id in your POST request to /admin/api/{version}/orders.json. The catch is the customer must already have a role assignment to that company location or it’ll error out.

For your 100K orders that are already imported, you’ve got two options:

  1. Use the admin UI migration feature to manually migrate the order history to company locations. Obviously not ideal at that scale.

  2. Delete the existing orders and re-import them using the REST endpoint with company data included from the start. Disruptive, but it’s the only programmatic way.

I know this isn’t the answer you were hoping for. The good news is this limitation is actively tracked as merchant feedback with our B2B team (multiple merchants have requested this capability), so there’s visibility on the need for a public API to handle post-import order migration.

If you want to add your voice to that feedback, feel free to share more details about your use case and I can make sure it gets passed along.

No GraphQL option? REST is deprecated.

1 Like

Good shout @Luke! There is a GraphQL option - it’s just not in the docs in the form of a tutorial like REST is.

The orderCreate mutation supports a companyLocationId field in its input that works the same way as the REST endpoint - I just tested it in my dev store and confirmed it works.

Here’s the GraphQL approach:

mutation orderCreate($order: OrderCreateOrderInput!) {
  orderCreate(order: $order) {
    order {
      id
      purchasingEntity {
        ... on PurchasingCompany {
          company { id name }
          location { id name }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

Variables:

{
  "order": {
    "lineItems": [{
      "title": "Product Name",
      "quantity": 1,
      "priceSet": {
        "shopMoney": {
          "amount": "10.00",
          "currencyCode": "CAD"
        }
      }
    }],
    "customer": {
      "toAssociate": {
        "id": "gid://shopify/Customer/12345"
      }
    },
    "companyLocationId": "gid://shopify/CompanyLocation/67890"
  }
}

Same requirement as REST: the customer needs a role assignment to that company location or you’ll get an error.

The limitation still stands though - this only works when creating new orders, not for migrating existing ones. The CompanyLocationMigrateOrders mutation Mark found is internal-only and there’s no public API equivalent for retroactively linking orders to companies.

So for 100K already-imported orders, the options are still either the admin UI’s migration tool or deleting and re-importing with company data from the start. The GraphQL method just gives an alternative to REST for the import approach. (I’ll submit feedback to get the docs updated to use GraphQL for the B2B order import tutorial, thanks again!)

Thanks Donal / Luke for your prompt replies

At least I have options to try

Many of the 100K orders are NOT companies (I hope) I can delete these. I will then try GQL creating companies then customers with role assignment to company then orders with the customer link.

Thanks for help
Mark