Reputation: 976
Everything was fine about line items in an invoice until now when I found it was not deleting all line items by providing an empty array in the Invoices.LineItems.
New line items are being added, existing line items are being updated or a single or more line items can be deleted as well. However, no changes to line items are made on Xero when passing an empty array in Invoices.LineItems. So how can we delete all the items from an invoice?
I have tested repeatedly by removing one item at a time and it gets deleted. The last item remains all the time.
The following screenshot was taken from Xero API Explorer after trying with a real invoice and a sample payload.
Upvotes: 0
Views: 169
Reputation: 976
After struggling in Xero API Explorer and reviewing no-line invoices, I came to a solution to delete all line items successfully from an Invoice.
Passing an empty object as the first/single index of the array Invoices.LineItems
does the job.
So instead of the following payload
{
"LineItems":[]
}
post the following payload to delete all line items from an invoice.
{
"LineItems":[{}]
}
However, to do that, I'll need to validate data and then remake the JSON body before submitting it to Xero. It's something, Xero should be the one doing that, by simply accepting the empty array []
for Invoices.LineItems
and fulfilling the request of deleting all line items.
Or, if this behaviour of providing empty object in the array [{}]
is intentional by Xero, it must be documented in the Xero API Reference. Because right now the third point of "Not providing an existing LineItem... deletes the line item" hints that simply an empty array []
would delete all line items, which is failing.
Upvotes: 1