2

Update with More Code:

The Test Class contains:

        Product2 plc = new Product2(Name='My Product', ProductCode='PC-1');
        insert plc;

        Id pricebookId = Test.getStandardPricebookId();

        PricebookEntry PLCpbe = new PricebookEntry(Product2Id=plc.id, UnitPrice = 200, Pricebook2Id=pricebookid, isActive = True);
        insert PLCpbe;

        Order ord = TestDataUtility.addorder('Test Order',ac.id,'Draft',con.id,null);
        ord.Pricebook2Id = pricebookid;

// This may have been my issue! This pricebookID was different than the one assigned to my Pricebook Entry. I'm getting new errors now, and will continue troubleshooting, but is this likely the issue? insert ord;


In my trigger code, I query for a PricebookEntry by ProductCode. Everything works great when I actually use it in the Test Org. and I'm creating a Test Class now, which is what I'm having an issue with.

The error I get is : INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

So I'm hoping somebody can review how I'm setting up a Product, Pricebook, and Pricebook Entry in a Test Class, and setting PBE to an OrderItem in case I'm doing something wrong.

In my Test class, I create a Product

Product2 plc = new Product2(Name='My Product', ProductCode='PC-1');
    insert plc;

I get a pricebook ID with

ID PricebookID = Test.getStandardPricebokID();

Then...

PricebookEntry PLCpbe = new PricebookEntry(Product2Id=plc.id, UnitPrice = 200, Pricebook2Id=pricebookid, isActive = True);
insert PLCpbe;

So, in my actual code, I query for a PricebookEntry by the product code and get a proper ID (not null, that is).

And try to create an OrderItem

OrderItem plcLineItem = New OrderItem(OrderId = o.id, PricebookEntryId = plcpbe.ID, Quantity = 1, UnitPrice = 200);

Like I said, this all works perfectly fine in actuality, but when I run my test class with this test data, I get the following error on upsert* of the OrderItem

INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

I've researched this and it seems, to my understanding, that this error means an ID was set to a field incorrectly, as in, it's not the right kind of ID somehow.

  • I upsert because in my code, I'm inserting some varying number of OrderItems, and updating others in some cases, so I add them all to a List and upsert all at once at the end.

Can somebody help identify where this error is coming from? I can't think of why an ID would be improper in this case. Let me know if you need any more information - thanks!

  • Did you insert the order before creating the line items? Perhaps OrderId is actually null? – sfdcfox Jul 13 '18 at 18:03
  • Mhm! I actually printed out the Order ID and the whole OrderItem to see it's values - no null values, the IDs make sense to me, so I figure I've got to be setting something up wrong. But yes, Order ID is not null, and I print out the PBE ID throughout the process, and it is what is assigned to the OrderItem. Stumped! – Natalie Paige Jul 13 '18 at 18:05
  • And the Order wouldn't be null because this is a trigger on Order :) So the top-level process is...I insert Order, change something about it (description), update it, and the trigger created the Oitems etc... – Natalie Paige Jul 13 '18 at 18:05
  • Can you post your code – Santanu Boral Jul 13 '18 at 19:30
  • 1
    I know you said there's a lot of code involved, but can you try to isolate a minimal example that shows this behavior? I'd love to be able to drop it in my developer org and troubleshoot. – David Reed Jul 16 '18 at 20:14
1

I found this interesting because the error message is rather inscrutable, so I put together a minimal example to demonstrate Natalie's analysis of the mistake. Here's a simple test class. We create two Orders, one with the test context's standard pricebook and one with a new pricebook. We then run a goodTest(), where we add an OrderItem a pricebook entry in the same pricebook as its Order, and a badTest(), where the OrderItem has a pricebook entry in a different pricebook.

Lastly, we run an otherGoodTest() where we insert an OrderItem on the non-standard Pricebook, but use a PricebookEntry that's also in that pricebook.

As predicted, goodTest() and otherGoodTest() pass and badTest() fails, confirming that this error stems solely from the mismatch in pricebooks.

@isTest
public class OrderTestTest {
    @testSetup
    public static void testSetup() {
        Account a = new Account(Name = 'Test');
        insert a;

        Product2 plc = new Product2(Name='My Product', ProductCode='PC-1');
        insert plc;

        PricebookEntry PLCpbe = new PricebookEntry(Product2Id = plc.id, UnitPrice = 200, Pricebook2Id = Test.getStandardPricebookId(), isActive = True);
        insert PLCpbe;

        Pricebook2 pb = new Pricebook2(Name = 'Pricebook');
        insert pb;

        Order ord = new Order(Name = 'Bad Order', AccountId = a.Id, EffectiveDate = Date.today(), Status = 'Draft', Pricebook2Id = pb.Id);
        insert ord;

        ord = new Order(Name = 'Good Order', AccountId = a.Id, EffectiveDate = Date.today(), Status = 'Draft', Pricebook2Id = Test.getStandardPricebookId());
        insert ord;

    }

    @isTest
    public static void badTest() {
        Order ord = [SELECT Id FROM Order WHERE Name = 'Bad Order'];
        PricebookEntry pbe = [SELECT Id FROM PricebookEntry];

        OrderItem plcLineItem = new OrderItem(OrderId = ord.id, PricebookEntryId = pbe.ID, Quantity = 1, UnitPrice = 200);

        insert plcLineItem;
    }

    @isTest
    public static void goodTest() {
        Order ord = [SELECT Id FROM Order WHERE Name = 'Good Order'];
        PricebookEntry pbe = [SELECT Id FROM PricebookEntry];

        OrderItem plcLineItem = new OrderItem(OrderId = ord.id, PricebookEntryId = pbe.ID, Quantity = 1, UnitPrice = 200);

        insert plcLineItem;
    }

    @isTest
    public static void otherGoodTest() {
        Order ord = [SELECT Id FROM Order WHERE Name = 'Bad Order'];
        PricebookEntry pbe = new PricebookEntry(
            Product2Id = [SELECT Id FROM Product2].Id, 
            UnitPrice = 300, 
            Pricebook2Id = [SELECT Id FROM Pricebook2 WHERE Name = 'Pricebook'].Id,
            isActive = True
        );
        insert pbe;

        OrderItem plcLineItem = new OrderItem(OrderId = ord.id, PricebookEntryId = pbe.ID, Quantity = 1, UnitPrice = 300);

        insert plcLineItem;
    }
}
0

I believe the error was in the Pricebook Entry ID on the Order - it was not the same as the one on the Order Item Pricebook Entry. When I changed this, I no longer got the error

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.