Apply
1 min
In this example, we are applying a Credit Note to an Invoice, but the same process can also be used for Bills with minor adjustments, as noted in the comments within the request.
To apply a Credit Note to an Invoice in Xero, you need to provide the unique IDs of both the Invoice and the Credit Note in the "InvoiceID" and "CreditNoteID" fields.
Set CreditNoteType to "Invoice" when applying it to an invoice, or "Bill" when applying it to a bill.
Note: When skipDML is set to false, the system performs DML operations and updates the Credit Note, Payment, and Invoice records in Salesforce.
// Create a List to store allocation data
List<Object> allocationList = new List<Object>();
Map<String, Object> allocationMap = new Map<String, Object>();
// Set the allocation amount (amount to apply from Credit Note to Invoice)
allocationMap.put('Amount', 10);
// Set the allocation date
allocationMap.put('Date', '2026-02-01');
// Initializing the Invoice to apply the allocation to
// This must be a valid existing record in Xero
Map<String, Object> invoiceMap = new Map<String, Object>();
// Note: InvoiceID can be either an Invoice or Bill ID from Xero
invoiceMap.put('InvoiceID', '72aac89b-a824-45d1-aa4d-0b9c59440917'); // Replace the ID with your valid Invoice or Bill ID
allocationMap.put('Invoice', invoiceMap); // As per Xero API, the key must always be 'Invoice' even when allocating to a Bill
// Add the allocation to the list
allocationList.add(allocationMap);
// Initializing the RequestJSON Data to be passed;
Map<String, Object> reqJSONMap = new Map<String, Object>();
// Note: CreditNoteID can be either an Invoice Credit Note or Bill Credit Note ID from Xero
reqJSONMap.put('CreditNoteID', 'd6f7a5ff-2297-4697-bdfd-f32bb54ce180'); // REQUIRED: Replace the ID with your Credit Note ID
reqJSONMap.put('CreditNoteType', 'Invoice'); // REQUIRED: Enter 'Invoice' for sales credit notes or 'Bill' for bill credit notes
// Attach allocation details to the request
reqJSONMap.put('Allocations', allocationList);
String reqJSON = JSON.serialize(reqJSONMap);
// Create the final request map
Map<String, Object> finalReqMap = new Map<String, Object>();
Map<String, Object> reqOptions = new Map<String, Object>();
finalReqMap.put('queryParameters', reqOptions);
finalReqMap.put('version', '1.0');
finalReqMap.put('action', 'applyCreditNoteAllocation');
finalReqMap.put('RequestJSON', reqJSON);
finalReqMap.put('skipDML', false); // skipDML = false → Applies the Credit Note to the Invoice/Bill and updates related records (Credit Note, Invoice, Payment)
// Required if Xero Multi-Org is enabled in Breadwinner
finalReqMap.put('xeroOrgId', '!3Mt9g'); // Replace with your Xero Org ID
System.debug('Request ::: ' + finalReqMap);
// Placing a request to Breadwinner Global API
Map<String, Object> respMap = BreadwinnerXeroAPI.call(finalReqMap);
System.debug('Response ::: ' + respMap);