I have a piece of code in my apex class which queries a work order with the corresponding Case ID.

I have written the corresponding test method for this class, as follows:

Case c = new Case();
insert c;
WorkOrder wrkOrdr = new WorkOrder(Case = c);
insert wrkOrdr;
WorkOrderLineItem wrkOrdrLineItm= new WorkOrderLineItem(WorkOrder = wrkOrdr);
insert wrkOrdrLineItm;

However, insert wrkOrdrLineItm throws an Exception:

System.DmlException: Insert failed.

First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

How can I correct my code to create a work order line item?


Your problem is how you set the parent:

new WorkOrderLineItem(WorkOrder = wrkOrdr);

When you set WorkOrder, you are using a fairly niche feature: Creating Parent and Child Records in a Single Statement Using Foreign Keys

The API Name of the field is WorkOrderId. The standard way to set a reference is by Id. So instead use:

new WorkOrderLineItem(WorkOrderId = wrkOrdr.Id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.