> For the complete documentation index, see [llms.txt](https://kb.logicerp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/orders/push/cancel-a-sale-order.md).

# Cancel a Sale Order

Cancels the **pending quantity of lines of a sale order** that is already saved in Logic ERP – for example when your customer cancels some items before they are delivered. You point to the order by Logic ERP's internal number returned when it was saved, or by your **party order number**, and list the lines to cancel. Only the quantity that is still pending is cancelled: what has already been billed, delivered, reserved or ordered from a supplier stays on the order. Cancelled lines are kept on the order, marked as cancelled. A cancel cannot be undone through the API.

### **Endpoint**

| Name          | Detail                                    |
| ------------- | ----------------------------------------- |
| Address       | `https://your-server/api/CancelSaleOrder` |
| Method        | `POST`                                    |
| Authorization | Basic Auth                                |

The response and the common messages are explained in [How Every API Call Works](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/how-every-api-call-works.md).

Send one order per request. Field names are matched without regard to upper/lower case; fields not listed below are ignored.

### **Request fields**

| Field             | Type   | Required                       | Default | Meaning                                                                                                                                                                                                                                                                                                            |
| ----------------- | ------ | ------------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Branch_Code`     | number | **Yes** (or `BranchShortName`) | `0`     | The branch the order belongs to (`Branch_Code` in [Get Branch Info](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/masters/pull/get-branch-info.md)). It must be the order's own branch, also when you send `SaleOrderCode`; an order of another branch is refused. |
| `BranchShortName` | text   | No                             | empty   | The branch by its short name. Used only when `Branch_Code` is `0`.                                                                                                                                                                                                                                                 |
| `SaleOrderCode`   | number | One of the two ways            | `0`     | Logic ERP's internal number of the order – the `LastSavedCode` you received from [Save a Sale Order](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/orders/push/save-a-sale-order.md). When it is above `0`, `PartyOrderNo` is not used. **Recommended.**           |
| `PartyOrderNo`    | text   | One of the two ways            | empty   | Your order number, as sent in `Party_Order_No` when the order was saved. Used when `SaleOrderCode` is `0`.                                                                                                                                                                                                         |
| `ListItems`       | list   | No                             | –       | The lines to cancel. See the table below. Leave it empty (or out) to cancel every pending line of the order, or only the items named in `OrderItemCodes`.                                                                                                                                                          |
| `Cancel_Reason`   | text   | No                             | empty   | Why the lines are cancelled. Stored with the cancellation, together with the date and time and the note *Cancelled From LOGIC Cancel Order API*.                                                                                                                                                                   |
| `OrderItemCodes`  | text   | No                             | empty   | Item codes separated by commas, for example `"KUR-BLU-M,KUR-RED-L"`, used only when `ListItems` is empty: every pending line of these items is cancelled. Each code must be an item code in Logic ERP (additional codes are not accepted here); spaces around a code and repeated codes are ignored.               |
| `DisplayError`    | text   | No                             | `"0"`   | `"1"` = when the cancel fails, `Message` carries the full technical detail instead of the short reason.                                                                                                                                                                                                            |

### **Request fields – ListItems**

| Field           | Type   | Required | Default | Meaning                                                                                                                                                                                                                                                                                                                       |
| --------------- | ------ | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ItemCode`      | text   | **Yes**  | –       | The item's code in Logic ERP (`LogicUser_Code` in [Get Item Master](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/masters/pull/get-item-master.md)). When your Logic ERP works with additional item codes, an additional code (for example the EAN barcode) is also accepted. |
| `Item_Order_ID` | text   | No       | empty   | The `Item_Order_ID` the line was saved with. When filled, only the order line with this ID is cancelled.                                                                                                                                                                                                                      |
| `User_Col_2`    | text   | No       | empty   | The `User_Column_2` value the line was saved with. Used when `Item_Order_ID` is empty: only the order lines of the item with this value are cancelled.                                                                                                                                                                        |
| `CancelQty`     | number | No       | –       | **Ignored.** The whole pending quantity of each matching line is cancelled.                                                                                                                                                                                                                                                   |

When neither `Item_Order_ID` nor `User_Col_2` is filled, **every line of that item** in the order is cancelled.

### **Rules Logic ERP applies**

**Finding the order**

* With `SaleOrderCode` above `0`, the order with that internal number is used.
* Otherwise the order with your `PartyOrderNo` in the branch `Branch_Code` is used. You must send at least one of the two. When more than one order in the branch has that party order number, the request is refused with *More than one Sale Order found against Party Order No. …. Please send the SaleOrderCode to Continue...* and nothing is cancelled.
* The order must belong to the branch you send, also when you send `SaleOrderCode`. Otherwise the request is refused with *Sale Order Code : … not found in Branch Code : …*.

**Finding the lines**

* Each entry of `ListItems` must match at least one line of the order, or nothing is cancelled and you get *Unable to found Item Code : …*.
* With `ListItems` empty, every pending line of the order is cancelled – or, when `OrderItemCodes` is filled, every pending line of those items. A code in `OrderItemCodes` that is not an item in Logic ERP is refused with *Item Codes : … not found. Cannot allow Sale Order Cancellation.*; an `OrderItemCodes` with no code in it is refused with *Please Define valid OrderItemCodes to Continue...*.
* Only lines that still have a **pending quantity** and are not already cancelled are cancelled. When none of the matching lines is pending, nothing is cancelled and you get *No Pending Item found in Sale Order for Cancel...Please Check*. When some listed items have no pending line, nothing is cancelled and the message names them: *Item Codes : … not exists in Order. Cannot allowed Sale Order Cancellation.*

**What blocks a cancel**

Nothing is changed when any of these applies:

* The whole order is already marked as cancelled in Logic ERP.
* The order has no configuration linked to it.
* The order was created from a branch order allocation.
* The order date is on or before the lock date for changes in Logic ERP.
* The order was changed by someone else while the cancel was running.

Approved orders **can** be cancelled.

**What a cancel does**

All of the following happen together, or none of them:

* For each matching line, the quantity that is still free is cancelled. Free means: not yet billed or delivered, not reserved, not used for production or assembly, and not ordered from a supplier.
  * A line with nothing used is marked as cancelled as a whole.
  * A line partly used is split: the used part stays on the line, the free part becomes a separate cancelled line with its share of the discounts and taxes.
  * A line fully used is left as it is.
* **The order itself is not marked as cancelled**, even when every line has been cancelled. In Logic ERP the order stays open, with its lines marked as cancelled.
* The cancellation is recorded with `Cancel_Reason`, the date and time, and an entry is written to the audit trail.
* When you list lines in `ListItems`, the order lines they match get `User_Column_5` set to `"CANCELLED"`.
* The cancel runs as the Logic ERP user linked to your API site, so that user's rights and locks apply.

This endpoint does not read your API site's endpoint settings, so switching it off there has no effect on it.

### **Example request**

By the internal number, cancelling one line by its ID and all pending lines of a second item:

```json
{
  "Branch_Code": 2,
  "SaleOrderCode": 50231,
  "Cancel_Reason": "Customer cancelled two items",
  "ListItems": [
    {
      "ItemCode": "KUR-BLU-M",
      "Item_Order_ID": "SHOP-100482-1"
    },
    {
      "ItemCode": "KUR-RED-L"
    }
  ]
}
```

By your party order number:

```json
{
  "Branch_Code": 2,
  "PartyOrderNo": "SHOP-100482",
  "Cancel_Reason": "Out of stock",
  "ListItems": [
    {
      "ItemCode": "KUR-BLU-M",
      "User_Col_2": "SIZE-M"
    }
  ]
}
```

### **Response**

| Field           | Type         | Meaning                                                                                                                                                                           |
| --------------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Status`        | true / false | `true` when the lines were cancelled.                                                                                                                                             |
| `Message`       | text         | `"Sale Order Cancelled Sucessfully..."` when cancelled; `"Sale Order not Cancelled..."` (with `Status: false`) when Logic ERP did not carry the cancel out; otherwise the reason. |
| `LastSavedCode` | number       | Always `0`.                                                                                                                                                                       |
| `request_ID`    | text         | As described on the common page. `null` when the request failed before the order was found.                                                                                       |

### **Example response**

```json
{
  "Status": true,
  "Message": "Sale Order Cancelled Sucessfully...",
  "LastSavedCode": 0,
  "request_ID": "8d41f0a2-5c3e-4b7a-9e16-0f2b7c94d3e5"
}
```

### **Messages you may see**

Some refusals come back as technical text with the reason inside it; the table says which. The reason may be followed by technical detail – read the first line.

| Message                                                                                                         | Meaning                                                                               | What to do                                                   |
| --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | ------------------------------------------------------------ |
| `Please Define PartyOrderNo / SaleOrderCode to Continue...`                                                     | Neither `SaleOrderCode` nor `PartyOrderNo` was sent.                                  | Send one of them.                                            |
| `Unable to found Sale Order againts Party Order No. … and Branch Code : …`                                      | No order with that party order number in the branch.                                  | Check `PartyOrderNo` and `Branch_Code`.                      |
| `More than one Sale Order found against Party Order No. …. Please send the SaleOrderCode to Continue...`        | More than one order in the branch has that party order number. Nothing was cancelled. | Send `SaleOrderCode`.                                        |
| `Sale Order Code : … not found in Branch Code : …`                                                              | No order has that `SaleOrderCode`, or the order belongs to another branch.            | Check `SaleOrderCode` and `Branch_Code`.                     |
| `Unable to found Item Code : …`                                                                                 | An entry of `ListItems` matches no line of the order.                                 | Check `ItemCode`, `Item_Order_ID` and `User_Col_2`.          |
| `Please Define valid OrderItemCodes to Continue...`                                                             | `OrderItemCodes` holds no item code (only commas or spaces).                          | Send item codes, or leave the field out.                     |
| `Item Codes : … not found. Cannot allow Sale Order Cancellation.`                                               | The named codes in `OrderItemCodes` are not items in Logic ERP.                       | Check the codes.                                             |
| `Sale Order not Cancelled...`                                                                                   | Logic ERP did not carry the cancel out. Nothing was cancelled.                        | Try again, or ask your Logic ERP team with the `request_ID`. |
| `Sale Order No.… is already Cancelled...`                                                                       | The whole order is already cancelled in Logic ERP.                                    | Nothing to do.                                               |
| `Configuration Not Linked againts Order...`                                                                     | The order has no configuration.                                                       | Cancel it in Logic ERP.                                      |
| `No Pending Item found in Sale Order for Cancel...Please Check`                                                 | The matching lines have nothing pending, or are already cancelled.                    | Nothing to cancel.                                           |
| `Item Codes : … not exists in Order. Cannot allowed Sale Order Cancellation.`                                   | The named items have no pending line in the order.                                    | Remove them from `ListItems`.                                |
| `This order has been created from Branch Order Allocation.... Cannot allow Modification.`                       | The order came from a branch order allocation.                                        | Ask your Logic ERP team.                                     |
| Technical text containing `Modification rights not available for this date.....`                                | The order date is on or before the lock date.                                         | Ask your Logic ERP team.                                     |
| Technical text containing `This Order has been Deleted by another` or `This Order has been modified by another` | `Branch_Code` is not the order's branch, or the order was changed while cancelling.   | Send the order's branch and try again.                       |
| Technical text containing `No Item Found With Pending Quantity`                                                 | The order has nothing pending.                                                        | Nothing to cancel.                                           |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/orders/push/cancel-a-sale-order.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
