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

INVALID_CROSS_REFERENCE_KEY when updating OpportunityLineItem

I'm about to tear my hair out over this... We have a basic trigger that updates the tax rate on the opportunity line items when the tax rate changes on the opportunity. We couldn't use a formula field because the calculated tax is rolled up into a roll-up summary field. However, I keep getting this error:


Message: Update failed. First exception on row 0 with id 00kE0000008QHZVIA4; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] Stack Trace: Class.optyTriggerClass.afterUpdateTrigger: line 61, column 1 Trigger.optyTrigger: line 7, column 1


In the trigger itself, and trigger.oldMap are passed into the method below:


public static void afterUpdateTrigger (List<Opportunity> optys, Map<ID, Opportunity> oldOptys) {
        Set<ID> optyIDs = new Set<ID>();
        for(Opportunity opty : optys) {
            Opportunity oldOpty = oldOptys.get(opty.Id);
            system.debug('Old Tax: ' + oldOpty.Tax_Rate__c);
            system.debug('New Tax: ' + opty.Tax_Rate__c);
            if(opty.HasOpportunityLineItem == true && (oldOpty.Tax_Rate__c != opty.Tax_Rate__c)) {
        if(!optyIDs.isEmpty()) {
            List<OpportunityLineItem> lineItems = [SELECT Id, OpportunityId, Opportunity.Tax_Rate__c, Tax_Rate_del__c, Taxable__c
                                                   FROM OpportunityLineItem 
                                                   WHERE OpportunityId IN :optyIDs];
            List<OpportunityLineItem> linesToUpdate = new List<OpportunityLineItem>();
            for(OpportunityLineItem oli : lineItems) {
                if(oli.Taxable__c == true) {
                    oli.Tax_Rate_del__c = oli.Opportunity.Tax_Rate__c;
            try {
                if(linesToUpdate.size() > 0) {
                    update linesToUpdate;
            } catch (Exception e) {
                system.debug('Message: ' + e.getMessage() + ' Stack Trace: ' + e.getStackTraceString());

The stack trace refers to the "update linesToUpdate" line, which is very unhelpful. I have no idea whatsoever why I am getting this error. If anyone could please help point out where this error is coming from I'd greatly appreciate it!

Best Answer chosen by Admin (Salesforce Developers) 

Nevermind this - turns out this was due to the PricebookEntry data not being available, an issue affecting a number of sandboxes according to this knowledge article.The trigger works fine in another, unaffected sandbox.