Deleting multiple records in a foundset
Posted: Thu Jul 14, 2022 2:57 am
When deleting records from a foundset, I have used the following approaches but I am still getting occasional "Update/insert failed, unexpected nr of records affected: expected 1, actual 0" exceptions. I have tried two different methods for achieving this, shown below; is there a preferred way of deleting multiple records from a foundset to avoid the exception being thrown?
and
- Code: Select all
function clearEmptyItems(recInvoice)
{
var numItems = 0;
var recItems = [];
if (utils.hasRecords(recInvoice.invoices_to_invoice_items))
{
numItems = recInvoice.invoices_to_invoice_items.getSize();
}
// As we will be deleting the records from the foundset, gather each of them so we don't rely on the (dynamic) index position
for (var index = 1; index <= numItems; index++)
{
var recItem = recInvoice.invoices_to_invoice_items.getRecord(index);
if (isItemEmpty(recItem))
{
recItems.push(recItem);
}
}
// Delete the item records from the foundset
recItems.forEach(function(recItemToDelete)
{
recInvoice.invoices_to_invoice_items.deleteRecord(recItemToDelete);
});
}
and
- Code: Select all
for (var index = 1; index <= recInvoice.invoices_to_invoice_items.getSize(); index++)
{
var recItem = recInvoice.invoices_to_invoice_items.getRecord(index);
if (isItemEmpty(recItem))
{
recInvoice.invoices_to_invoice_items.deleteRecord(index);
index--;
}
}