---
title: Create and Apply Credit Note
slug: xero/create-and-apply-credit-note
docTags: 
createdAt: 2026-02-13T12:50:32.671Z
---

In this example, we first create a Credit Note and then immediately apply it to an Invoice within a single request without requiring separate manual steps. This process involves two actions:

1. Creating the Credit Note with the required details such as contact, line items, and amount.&#x20;
2. Extracting the generated CreditNoteID from the response and using it to apply the Credit Note to an existing Invoice by specifying the InvoiceID and Allocation amount.&#x20;

Two separate responses are returned as part of this process:

- Create Response: contains the details of the newly created Credit Note, including the CreditNoteID.&#x20;
- Apply Response: contains the allocation details showing how the Credit Note was applied to the Invoice.

:::CodeblockTabs
Request

```apex
// STEP 1: Create Credit Note

List<Object> creditNoteList = new List<Object>();
Map<String, Object> creditNoteMap = new Map<String, Object>();

// REQUIRED: Credit Note type
// Use 'ACCRECCREDIT' for Sales Credit Note
// Use 'ACCPAYCREDIT' for Bill Credit Note
creditNoteMap.put('Type', 'ACCRECCREDIT');

creditNoteMap.put('Date', '2026-11-14'); // Set the Credit Note Date
creditNoteMap.put('LineAmountTypes', 'Exclusive'); // Line amount type (Exclusive / Inclusive / NoTax)
creditNoteMap.put('Status', 'AUTHORISED'); // Status of the Credit Note

//Contact Information
Map<String, Object> contactMap = new Map<String, Object>();
contactMap.put('ContactID', 'cf8dd2e8-32b7-42ed-bdd7-c2021576d789');
creditNoteMap.put('Contact', contactMap);

// Credit Note Line Items
List<Object> lineItemList = new List<Object>();
Map<String, Object> lineItemMap = new Map<String, Object>();
lineItemMap.put('Description', 'Line Item Description');
lineItemMap.put('Quantity', 10);
lineItemMap.put('UnitAmount', 100);
lineItemMap.put('AccountCode', '200');

lineItemList.add(lineItemMap);
creditNoteMap.put('LineItems', lineItemList);

// Add Credit Note to list
creditNoteList.add(creditNoteMap);

// Request JSON
Map<String, Object> reqJSONMap = new Map<String, Object>();
reqJSONMap.put('creditnotes', creditNoteList);

String reqJSON = JSON.serialize(reqJSONMap);

// Final request
Map<String, Object> finalReqMap = new Map<String, Object>();
finalReqMap.put('queryParameters', new Map<String, Object>());
finalReqMap.put('version', '1.0');
finalReqMap.put('action', 'createCreditNote');
finalReqMap.put('RequestJSON', reqJSON);
finalReqMap.put('skipDML', true);
finalReqMap.put('xeroOrgId', '!n5t1z'); // Replace with your Xero Org ID

Map<String, Object> createResp = bread_winner.BreadwinnerXeroAPI.call(finalReqMap);
System.debug('Create Response ::: ' + createResp);

// STEP 2: Extract CreditNoteID from response (FIXED)

// responseJSON comes as String → must deserialize
String responseStr = (String) createResp.get('responseJSON');

// Convert String → Map
Map<String, Object> responseJSON = (Map<String, Object>) JSON.deserializeUntyped(responseStr);

// Extract creditnotes list
List<Object> creditNotes = (List<Object>) responseJSON.get('creditNotes');

// Get first Credit Note record
Map<String, Object> createdCN = (Map<String, Object>) creditNotes[0];

// Extract CreditNoteID
String creditNoteID = (String) createdCN.get('CreditNoteID');

// STEP 3: Apply Credit Note

List<Object> allocationList = new List<Object>();
Map<String, Object> allocationMap = new Map<String, Object>();

// Allocation details
allocationMap.put('Amount', 10); // Set the allocation amount (amount to apply from Credit Note to Invoice)
allocationMap.put('Date', '2026-11-14'); // Set the allocation date

// Invoice / Bill reference ID
Map<String, Object> invoiceMap = new Map<String, Object>();
invoiceMap.put('InvoiceID', 'd238e0b3-34e7-41df-9168-8a944bda4b8e'); // Replace the ID with your valid Invoice or Bill ID

// Key must always be 'Invoice'
allocationMap.put('Invoice', invoiceMap);

// Add allocation to list
allocationList.add(allocationMap);

// Allocation request JSON
Map<String, Object> allocReqJSONMap = new Map<String, Object>();
allocReqJSONMap.put('CreditNoteID', creditNoteID);
allocReqJSONMap.put('CreditNoteType', 'Invoice'); // REQUIRED: Enter 'Invoice' for Sales credit notes or 'Bill' for Bill credit notes
allocReqJSONMap.put('Allocations', allocationList);

String allocReqJSON = JSON.serialize(allocReqJSONMap);

// Final allocation request
Map<String, Object> finalAllocReq = new Map<String, Object>();
finalAllocReq.put('queryParameters', new Map<String, Object>());
finalAllocReq.put('version', '1.0');
finalAllocReq.put('action', 'applyCreditNoteAllocation');
finalAllocReq.put('RequestJSON', allocReqJSON);
finalAllocReq.put('skipDML', false); // skipDML = false → Applies the Credit Note to the Invoice/Bill and updates related records (Credit Note, Invoice, Payment) in Salesforce
finalAllocReq.put('xeroOrgId', '!n5t1z'); // Replace with your Xero Org ID

// Call allocation API
Map<String, Object> allocResp = bread_winner.BreadwinnerXeroAPI.call(finalAllocReq);
```

Create Response

```json
{
   "action=createCreditNote",
   "responseJSON="{
      "creditNotes":[
         {
            "Url":null,
            "UpdatedDateUTC":null,
            "Type":"ACCRECCREDIT",
            "TotalTax":0.00,
            "TotalDiscount":null,
            "Total":1000.00,
            "Telephone":"",
            "SubTotal":1000.00,
            "Status":"AUTHORISED",
            "SentToContact":null,
            "SalesforceRecordId":null,
            "Salesforce_Opportunity_Id":null,
            "Salesforce_Account_Id":null,
            "RemainingCredit":1000.00,
            "Reference":"",
            "PurchaseOrderNumber":null,
            "PurchaseOrderID":null,
            "Payments":[
               
            ],
            "LineItems":[
               {
                  "UnitAmount":100.00,
                  "Tracking":[
                     
                  ],
                  "TaxType":"OUTPUT",
                  "TaxAmount":0.00,
                  "Quantity":10.0000,
                  "LineItemID":"f61e7139-d002-495c-a6ca-984ce5080fec",
                  "LineAmount":1000.00,
                  "ItemCode":null,
                  "DiscountRate":null,
                  "Description":"Line Item Description",
                  "AccountCode":"200"
               }
            ],
            "LineAmountTypes":"Exclusive",
            "IsDiscounted":null,
            "InvoiceNumber":null,
            "InvoiceID":null,
            "HasErrors":false,
            "DueDateString":null,
            "DueDate":null,
            "DeliveryInstructions":"",
            "DeliveryDateString":null,
            "DeliveryDate":null,
            "DeliveryAddress":"",
            "DateVariable":"/Date(1794614400000+0000)/",
            "DateString":"2026-11-14T00:00:00",
            "CurrencyRate":1.0000000000,
            "CurrencyCode":"INR",
            "CreditNotes":null,
            "CreditNoteNumber":"CN-0033",
            "CreditNoteID":"62991bcb-b3e6-4b56-8a46-e3d76ebf32a2",
            "Contact":{
               "Name":"Test",
               "ContactStatus":"ACTIVE",
               "ContactID":"cf8dd2e8-32b7-42ed-bdd7-c2021576d789"
            },
            "BrandingThemeID":"c405b1aa-f5a1-479b-98c5-1c3e39895129",
            "AttentionTo":"",
            "AmountPaid":null,
            "AmountDue":null,
            "AmountCredited":0.00,
            "Allocations":[
               
            ]
         }
      ],
      "errors":[
         
      ],
      "statusCode":200
   },
   "skipDML=true",
   timestamp=1775484106615,
   "validRequest=true",
   version=1.0
}
```

Apply Response

```apex
{
   "action=applyCreditNoteAllocation",
   "responseJSON="{
      "allocations":[
         {
            "Invoice":{
               "InvoiceNumber":null,
               "InvoiceID":"d238e0b3-34e7-41df-9168-8a944bda4b8e"
            },
            "DateVariable":"/Date(1794614400000+0000)/",
            "CreditNote":{
               "Total":null,
               "RemainingCredit":null,
               "Payments":null,
               "ID":"be3e8078-f85a-4485-ac28-1b16abfce67c",
               "DateVariable":null,
               "DateString":null,
               "CreditNoteNumber":null,
               "CreditNoteID":"be3e8078-f85a-4485-ac28-1b16abfce67c",
               "AppliedAmount":null
            },
            "Amount":10.00,
            "AllocationID":"7710dae9-79b3-42fd-84bc-e53eec74e9af"
         }
      ],
      "errors":[
         
      ],
      "statusCode":200
   },
   timestamp=1775553758023,
   "validRequest=true",
   version=1.0
}
```
:::

