I am getting the error "INVALID_CROSS_REFERENCE_KEY, invalid cross reference id:" while trying to insert an Order Item related to an order, by cloning the opportunity line item records using an Apex Trigger.

I understand the most common reason for this to come up if the related order record doesn't have a Price Book mentioned on it.

I have taken care of that. The peculiar thing here is that the apex trigger seems to fire with out any error for just one price book, but while it throws this error for all the other price books. I seem to be dumbfounded with the reason for it.

My apex trigger code is as follows:

        oliAll = [select id, Name, OpportunityId, PriceBookEntryId, PriceBookEntry.PriceBook2Id, Discount, Description, ListPrice, ProductCode, Quantity, UnitPrice, Subtotal, TotalPrice, Sale_Type__c from OpportunityLineItem where opportunityId IN: oppWonId];    

        for(Opportunity opp: oppWon){
            Order ord = new Order();
            ord.OpportunityId = opp.Id;
            ord.AccountId = opp.AccountId;
            ord.name = 'Order - ' + opp.name;
            ord.Type = 'New Business';
            ord.RecordTypeId = '012400000005gGm';
            ord.EffectiveDate = opp.CloseDate;
            //ord.EndDate = opp.Evaluation_End_Date_New__c;
            ord.Multiple_Shipping_Addresses__c = opp.Multiple_Shipping_Addresses__c;
            ord.PoNumber = opp.Shipment_Tracking_Number__c;
            ord.status = 'Draft';
            //ord.TotalAmount = opp.Amount;
            if(opp.Pricebook2Id != Null){
                ord.Pricebook2Id = opp.Pricebook2Id;

        insert ordTBU;
        for(Order o : ordTBU){
            for(OpportunityLineItem oli: oliAll){
                if(oli.OpportunityId == o.OpportunityId){
                    system.debug('Order Price Book Id IS:'+o.PriceBook2Id);
                    system.debug('Opportunity Line Item Price Book ID is:'+oli.PriceBookEntry.PriceBook2Id);
                    OrderItem op = new OrderItem();

                    op.PricebookEntryId = oli.PriceBookEntryId;
                    system.debug('Opportunity Line Item Price Book Entry ID is:'+oli.PriceBookEntryId);
                    system.debug('Order Item Price Book Entry ID is:'+op.PriceBookEntryId);
                    op.quantity = oli.Quantity;
                    op.orderId = o.id ;
                    op.UnitPrice = oli.UnitPrice;
                    system.debug('Order Item Price Book Id Is:'+op.PriceBookEntry.PriceBook2Id);
            ServiceContract sc = new ServiceContract();
            sc.Related_Order__c = o.id;
            sc.Type__c = 'Production';
            sc.Name = o.name;
            sc.AccountId = o.AccountId;


        system.debug('Order Item List:'+ordProdTBU);
        insert ordProdTBU;
  • do you use multicurrency ?do yo use with sharing class ?Can you share how you are calling this trigger and class permissions Apr 30, 2015 at 16:41
  • Yes Mohith, we use sharing currencies, and its a stand alone trigger, that fires every time the opportunity stage is updated to CLOSED WON. It is supposed to execute 5 scenarios, while it is only executing one scenario right now. Apr 30, 2015 at 19:03

2 Answers 2


Make sure you RecordTypeId exists in the current Org. Many times, when you hardcode values, the ID doesn't exist in the Org.

I recommend using this to obtain the ID, which will work in any org:

ID myRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('myCustomRecordTypeName').getRecordTypeId();
  • James good point .Was about to convey same to the author of the question .+1 from me Apr 30, 2015 at 16:49
  • Hi James, YES the record type id exists. The entire process works flawlessly when i Use a particular price book "X", but it falls apart totally when i use any other price book on opportunity. Do you see any connection between pricebooks and Order Items?? Apr 30, 2015 at 19:01

I get this error when I try insert one OrderItem when the PriceListId from Order was the different of PriceList that I use on OrderItem.

Then verify PriceLisId between Order and OrderItem, in my case this was problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .