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
sf42_Tobisf42_Tobi 

Problem deleting Opportunity Line Items - Error: INVALID_CROSS_REFERENCE_KEY

Hi,

 

I've implemented a function to replace the current OpportunityLineItems by a list of custom objects. The method works fine most the time, but since one week, I receive error Mails from time to time.

 

System.DmlException: Delete failed. First exception on row 0 with id 00k2000000xxXxxXXX; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id

 

I tried many times, but wasn't able to reproduce this error. But it occurs from different users with different profiles. When I try to call the Object with the ID from the Email, salesforce says "data not available", so the delete must have worked in a way. Here is a code snippet, the selectedLineItems are generated on page load, the method is called from a button inside the visualforce page:

 

 

public void copyLineItems() {
id opptyId = quote.SF42_QuoOpportunity__r.id;
if (opptyId == null) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,err_noO));
}
else if (selectedLineItems == null) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,err_q2o));
}
else {
// get actual OppLineItems
List<OpportunityLineItem> oldLineItems = [Select id from OpportunityLineItem where opportunityId = :opptyId];
// create new OppLineItems
List<OpportunityLineItem> newLineItems = new List<OpportunityLineItem>();
for (SF42_GenLineItem__c s: selectedLineItems){
if (s.SF42_QuoLI_PriceBookEntryId__c != NULL) {
newLineItems.add(New OpportunityLineItem(
OpportunityId = opptyId,
Quantity = (double) s.get('Quantity__c'),
UnitPrice = (Double) s.get('Unitprice__c'),
Description = (string)s.get('Description__c'),
pricebookEntryId = (Id) s.get('pricebookEntryId__c')
));
}
}
// replace current line items with Opp line items and update Opp
if (newLineItems.size() > 0) {

// ERROR OCCURS IN THE FOLLOWING LINE

delete oldLineItems;

insert newLineItems;
opp.Field1__c = myobject.Field1__c;
opp.Field2__c = myobject.Field2__c;
opp.Field3__c = myobject.Field3__c;
update opp;
}
else {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,err_q2o));
}
}
}

 Can anyone give advice to this problem?

 

Thanks in advance,

Tobias

 

 

Tim BarsottiTim Barsotti

I know this post is really really old but I just ran into a similar problem and tried to find a solution. 

 

I would ensure the deleteOldItems.size() > 0 before you call the delete function... did you find another solution?

Shivam Mishra 36Shivam Mishra 36
You can do one thing delete the previous one data and insert the new data as fresh data.


As a Example:-

List<OpportunityLineItem> oppy=[Select OpportunityID From OpportunityLineItem WHERE OpportunityId = :OppId];
        delete oppy;

It will delete the previous data. and then insert the new data.