I have a relatively simple script to delete all the quotes found by a particular search:
function executeSavedSearch(){ var searchresults = nlapiSearchRecord('estimate', 1234, null, null); for (var i = 0; searchresults != null && i < searchresults.length; i++) { try { nlapiLogExecution("Debug", "About to delete record id " + id); nlapiDeleteRecord(searchresults[i].getRecordType(), searchresults[i].getId()); } catch (err) { var errMessage = err; if(err instanceof nlobjError) { errMessage = errMessage + ' ' + err.getDetails() + ' ' + 'Failed to Delete ID : ' + id; } nlapiLogExecution('ERROR', 'Error', errMessage); return err } }}The problem is that it always fails with a RCRD_DSNT_EXIST error. I put in some debugging, and the ID of the record it first tries to delete is actually valid; more, it does actually delete it. But it throws an error anyway.
So the log entry (line 8 above) says it's about to delete record 1234. The nlapiDeleteRecord call throws an exception and the catch block logs the message:
Code: RCRD_DSNT_EXIST Details: That record does not exist. That record does not exist. Failed to Delete ID : 1234
But when I go back to my search, I find that record 1234 has actually been deleted. If I run the script again, it'll find record 1235 as the first one to delete and find that that record doesn't exist - but it still be deleted.
It's almost as if the nlapiDeleteRecord tries to delete the record twice, failing on the second delete because it's already deleted it.
I've thoroughly checked the search and the problem isn't there - it definitely only returns one row per transaction. If I comment out the call to nlapiDeleteRecord, it logs exactly what I'd expect - a single log entry for the ID of each of the quotes I want deleted.
Any ideas?