function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BlobBlob 

Deleting a missing id returns INVALID_CROSS_REFERENCE_KEY

I have a case where I'm deleting OpportunityLineItem, PricebookEntry, and a Product2 instances that are related to each other. I try to delete them in one call to the delete API with the ids in the above mentioned order.

The OpportunityLineItem is successfully deleted on the first try.
The PricebookEntry was removed, but returned as unsuccessful in the DeleteResult.
On the second delete try with only the PricebookEntry (already deleted) and Product2 ids the Product2 id is successfully deleted, but the PricebookEntry returns a INVALID_CROSS_REFERENCE_KEY.

Why is the PricebookEntry DeleteResult return unsuccessful in the first attempt?
Why is it that when I try to delete an already missing id that the DeleteResult is unsuccessful?

It seems to me that if the id is not there it does not need to be removed anylonger and the delete can return a success.

I used SForce Explorer to determine that the id was in fact removed.
SuperfellSuperfell
There was an unintential change in the winter 06 release with regard to the order in which the deletes get done, which cause it to fail now when it didn't used to. This fix for this should be going out soon. If you log a case with support for bug #85432, they'll be able to notify you when the fix is live.
SuperfellSuperfell
The fix for this is live.
BlobBlob
I am still getting the "INVALID_CROSS_REFERENCE_KEY - id does not exist" message.

I don't understand why the delete needs to throw an error if the id to delete does not exist. If the id is not there then the job is already done and the delete should be successful.

- Peter
SuperfellSuperfell
WHich API version are you using ? in 6.0 and below this should succeed without error. In 7.0 validation was tightened up.
BlobBlob
I am currently using version 7.0.
BlobBlob
I am still getting the "INVALID_CROSS_REFERENCE_KEY - id does not exist" message with 7.0 API.

What did you mean with in 7.0 validation was tightened up?

- Peter
SuperfellSuperfell
It works in 6.0 because it lets you delete things that are already deleted and report success, this is no longer the case in 7.0.
BlobBlob
1. In my case I have 3 instance created, opportunitylineitem, pricebookentry, product2 and they are in that order when I call the delete API. Two ids are reported as successfully deleted, one is reported as not successfully deleted, but is really was deleted. This seems like a bug in the 7.0 API to me.

2. The next time I try to delete the remaining id that had the unsuccessful result and it returns the same error. This is in effect trying to remove an id that does not exist and returns the same error as the first time. I think this is a bug also, but as you said this is no longer the case in 7.0 and we will have to work around it.

The bug in 1 is the cause of the situation in 2. I think that the delete method in 7.0 needs to be reviewed again. Please consider returning a success if somebody tries to delete an already deleted id.

- Peter
SuperfellSuperfell
what's likely happening is one of the deletes cascade deletes one of the other ones, so when it comes to that one, its already deleted, you should be able to just pass up the 2 sucessfully deleted ids, to have all 3 records deleted (I'd have to double check, but would guess that the oppline item is cascade deleted from the pricebookentry delete).