# setSalesOrderInvoice mutation


`setSalesOrderInvoice` is a GraphQL mutation of the Chameleoon MAX API. Send it as `POST https://max-api.chameleoon.sk/api/v1/graphql` with your API key, see [Chameleoon MAX](/chameleoon-max).

Records the invoice that another system issued for the order. Call it after your accounting
system or e-shop issues the invoice. It works on a confirmed or completed order.

A field you omit is kept. Null or blank clears it.

#### Permissions

Requires the `order.write` and `order.read` permissions.

#### Repeated calls

When nothing differs, the result is `changed: false`. MAX checks this before
`expectedExternalId`, so a retry after a lost response is not a conflict.

#### Errors

MAX checks the errors in this order:

1. `CONFLICT` with `extensions.reason` `ORDER_STATUS` when the order is a draft, cancelled or
   merged.
2. `VALIDATION` (field `invoice.number` or `invoice.externalId`) for a number over 100
   characters or an external id over 200 characters.
3. `CONFLICT` with `extensions.reason` `INVOICE_GOVERNED` (see below). It is checked before
   `INVOICE_CHANGED`, so a stale expectation does not hide it.
4. `CONFLICT` with `extensions.reason` `INVOICE_CHANGED` when the order's invoice is not the
   one you expected. Read the order again. Nothing is written.

`NOT_FOUND` when the order does not exist or your API key cannot see it.

#### Invoices linked in MAX

While an invoice linked to the order in MAX governs the order's invoice reference, the
reference is that invoice's.

- A different number, or clearing the number, returns `CONFLICT` with `extensions.reason`
  `INVOICE_GOVERNED` and `extensions.invoiceNumber` set to the governing number.
- A write with the governing number returns `changed: false`, whatever its `externalId` and
  `expectedExternalId`. So does a write of `externalId` alone. Nothing is written.

#### Good to know

- The order's history records every change. A change of `invoiceNumber` is also a transition
  in the order's events. `invoiceExternalId` is not tracked there, so a change of it alone
  still publishes `sales_order.changed`, with no transition.
- Attach the invoice's PDF to the order as an `INVOICE` document
  ([`attachDocument`](/chameleoon-max/mutation/attach-document)) and read it from `documents`. Its `externalNumber` is the
  invoice number and its `downloadUrl` is the PDF.
- For an invoice imported from the accounting system, the PDF is kept with the invoice
  instead ([`attachInvoiceDocument`](/chameleoon-max/mutation/attach-invoice-document)) and is read from `Invoice.documents`.

> An error whose path runs below this field does not mean the call failed: the invoice was
> recorded, so do not retry. See [`Mutation`](/chameleoon-max/mutations).

## Arguments

- `ref`: [`SalesOrderRefInput!`](/chameleoon-max/type/sales-order-ref-input) · required
- `invoice`: [`SalesOrderInvoiceInput!`](/chameleoon-max/type/sales-order-invoice-input) · required

## Returns

[`SalesOrderChangeResult!`](/chameleoon-max/type/sales-order-change-result)

## Example

**Mutation**

```graphql
mutation setSalesOrderInvoice($ref: SalesOrderRefInput!, $invoice: SalesOrderInvoiceInput!) {
  setSalesOrderInvoice(ref: $ref, invoice: $invoice) {
    salesOrder {
      id
      number
      status
      legalEntityId
      source
      externalId
    }
    changed
  }
}
```

**Variables**

```json
{
  "ref": {
    "id": "00000000-0000-4000-8000-000000000001"
  },
  "invoice": {}
}
```

**Response**

```json
{
  "data": {
    "setSalesOrderInvoice": {
      "salesOrder": {
        "id": "00000000-0000-4000-8000-000000000001",
        "number": "example",
        "status": "DRAFT",
        "legalEntityId": "00000000-0000-4000-8000-000000000001",
        "source": "example",
        "externalId": "example"
      },
      "changed": true
    }
  }
}
```

Values are illustrative: replace IDs and codes with your own before you send the call.
