Create and Apply Credit Note
1 min
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 creating the credit note with the required details such as contact, line items, and amount 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 two separate responses are returned as part of this process create response contains the details of the newly created credit note, including the creditnoteid apply response contains the allocation details showing how the credit note was applied to the invoice request // 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 { "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 { "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 }
