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
Glenn at MvistaGlenn at Mvista 

OrderItem product2 read only

In trying to utilize the Order and OrderItem objects, I want to create an Order record from an Opportunity that is moved to Booked/Ordered Stage.  That is done, no problem.

However, when I try to then replicate the line items from the Opportunity over to the Order record, using the bare minimum, Qty, Price, Product, I get the following UI error:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Order3_OrderItemCreate caused an unexpected exception, contact your administrator: Order3_OrderItemCreate: 
execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: 
INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []: 
Trigger.Order3_OrderItemCreate: line 47, column 1
I have narrowed the access  error down to the Product field.  In the trigger, I am using the PriceBookEntryId (same as I have done in the past for ServiceContract/ContractLineItem objects.

Looking at the OrderItem and the ContractLineItem objects, I can see that Product2 is READ ONLY on OrderItem, but not in ContractLineItem.  How can I change this or code around it?
Best Answer chosen by Glenn at Mvista
Glenn at MvistaGlenn at Mvista
OK, I found the answer.  Dumb mistake on my part.  I did not include the OrderId in the insert so the record could not be created.

All Answers

Peter_sfdcPeter_sfdc
Just to be clear, I've not worked with this specific feature. And I don't konw what your code is doing, so I'm flying blind with both eyes here. 

But having turned this on in my org, what I see is that OrderItem.PriceBookEntryId has the Creatable attribute set to true, but the Updateable attribute set to false. 

Ostensibly, what this would mean would be that you could insert a new OrderItem record with PriceBookEntryId, but if any of your code attempted to update this field in any way, you would receive an error. 

In the act of replicating, are any of your flows attempting to perform an update or upsert operation? 
Glenn at MvistaGlenn at Mvista
Peter,

Thanks for helping.  The Trigger I have for inserting the line items is triggered off a change on the Order itself.  It is only performing an insert.  Here is the complete trigger (note: when working, it will only be an "after insert" trigger.  I have after update in place now just so I can update an Order and test if the line items get created).

trigger Order3_OrderItemCreate on Order (after insert, before update) {

// ERROR - I believe the problem is READ ONLY product field on OrderItem - put question to 
// Community 6/6/2014 : 
// https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AXWbIAO

    // PULL SETS OF OPPORTUNITY IDS
    Set<id> Opp_Ids = new Set<id>();
    for (Order sc : trigger.new) {
        Opp_Ids.add(sc.Opportunity__c);
    }

    // PULL AN ARRAY OF ALL OPPORTUNITY LINE ITEM RECORDS
    OpportunityLineItem[] OLI_Array = [Select OpportunityId, 
    		Id,
			Quantity,
			UnitPrice,
			PriceBookEntryId
		from OpportunityLineItem where OpportunityId in :Opp_Ids];

    // CREATE A OPPORTUNITYLINEITEM MAP AND FILL IT WITH THE ARRAY
    Map<id, OpportunityLineItem> OLI_Map = new Map<id, OpportunityLineItem>();
    for (OpportunityLineItem olimap : OLI_Array)
        OLI_Map.put(olimap.Id, olimap);

    // Create an asset for each line item on the opportunity     
    OrderItem[] clis = new OrderItem[]{};

	for (Order sc : trigger.new) {
		string opptyId = sc.Opportunity__c;
		
        for (OpportunityLineItem oli : OLI_Array) {
        	system.debug('DEBUG CHECK ID : ' + OLI_Map.get(oli.Id).PriceBookEntryId);
            // IF THE RECORDS ARE FOR THE CURRENT OPPORTUNITY, THEN CREATE OBJECT, BUT DON'T INSERT YET
            if (oli.OpportunityId == opptyId) {
		        OrderItem cli = new OrderItem();
		        cli = new OrderItem();
		        	cli.UnitPrice = OLI_Map.get(oli.Id).UnitPrice;
		        	cli.Quantity = OLI_Map.get(oli.Id).Quantity;
                    cli.PriceBookEntryId = OLI_Map.get(oli.Id).PriceBookEntryId;
                clis.add(cli);
            }
		}
    }
    // INSERT ACTUAL RECORDS
    insert clis;

}


Glenn at MvistaGlenn at Mvista
Peter,

Hold on a bit.  I think there is a problem with the associated Price Book itself.  The original trigger that created the order may be the source of the downstream error.  I will work on this and update here if I need assitance.

Glenn
Glenn at MvistaGlenn at Mvista
Nope, that was not it. Back to original problem.
Glenn at MvistaGlenn at Mvista
OK, I found the answer.  Dumb mistake on my part.  I did not include the OrderId in the insert so the record could not be created.
This was selected as the best answer