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
Pedro Garcia GPedro Garcia G 

Unit test with Order Product object

Hi...

I'm writing my unit test of the CustomerOrder class. This class has a method which returns all Orders with status = Available.

In the CustomerOrderTest class, I can not create the data for testing because I don't have access to OrderItem object. I have the System Administrator profile.
 
@isTest
    public static void hasTrackingNumberFalseTest(){
                    Product2 prd = new Product2();
        
        prd.name ='Product Express';
        
        orderItem oi = new OrderItem();
        
        order o = new Order();
        o.EffectiveDate = Date.newInstance(2019, 3, 23);
        o.Status = 'Draft';
        
        Account ac = new Account(name='Test Account');
        Test.startTest();
        
        insert prd;
        insert ac;
        oi.Product2Id = prd.Id;
        oi.UnitPrice  = 30;
        insert oi;
        
        o.AccountId = ac.Id;
        Database.SaveResult result = Database.insert(o);
        oi.OrderId = o.Id;
        System.debug('>>>'+result);
        
        System.assertEquals(true,String.isBlank(OrderAuraController.hasTrackingNumber(o)));

        
        System.debug('>>>'+o);
        
        o.Status = 'Shipped';
        
        Database.SaveResult result2 = Database.update(o);
        System.assertEquals(true,OrderAuraController.hasTrackingNumber(o).equals('OrderAuraController.hasTrackingNumber(o)'));
        
        System.assert(!result2.isSuccess());
        System.assert(result2.getErrors().size() > 0);
        System.assertEquals('The Order Status can not be Processed until the Trackin Number is entered.',
                             result2.getErrors()[0].getMessage());
        Test.stopTest();
        

        

    }


Questions:
How could I create test data if I don't have access to this object?
How could I give access to System Administrator project for OrderItem object?

Thanks,
ApuroopApuroop
Hello,

Since you can view the data, by using the annotation (seeAllData=true), get the list of orders using SOQL based on some criteria. Use this list in your test.
For giving access to the object, you have to clone the System Administrator profile and then give Modify All for that object. Although, I don't see any object in my System Admin profile without the ModifyAll permissions. 
Pedro Garcia GPedro Garcia G
Hi Apuroop,

Thank you for your reply. The SF deploy recommendations recommend to avoid the  (seeAllData=true) for deployment in Production.

Yes, you're right... I don't know why the System Admin profile doesn't have access to ModifyAll in Order Product in our organization.

 
ApuroopApuroop
Whenever you deal with (seeAllData=true) annotation, if you make any DML operations on the very object, the changes won't apply outside the test class. Here's an example:
 
@isTest(seeAllData=true)
public class Acc_test {
    public static testmethod void testMeth1(){
        List<Account> accs = [SELECT id, name FROM Account WHERE name = 'test1'];
        System.debug('>>>'+accs[0].name);
        accs[0].name = 'After update';
        update accs;
        System.debug('>>>'+accs[0].name);
    }
}
In the debug log, you'll see the change but if you go to the record at UI level, the account's name will still be test1.

I don't see why not you use this list, since it's just for the sake of testing.

Also, this is how I created an Order Product in my developer org, Create an Account > Create a Contract > Create an Order > From the Order's related list, create an Order Product (OrderItem)

Can you see the Order Product related list on Order detail page? My thinking is that, since it's a standard object - it should be same across all editions unless your org is setup in a different way.