0

I am trying to cover the following condition in my Apex test class and the ActivatedByID and ActivatedDate are always being set as null. In the test class, I created an order, set it's status as 'Draft' and then updated it to 'Activated'. The status gets changed as expected but the Activation date and ID remail null. Is there any way to set these fields via test class?

if((newOrder.RecordType.DeveloperName == 'Activated' || newOrder.m3Status__c 
    =='orderActivated') && newOrder.ActivatedById != null && 
    newOrder.ActivatedDate != null && newOrder.Offhire_Date__c != null && 
    newOrder.Terminated_Date__c == null)
            {
                    ordRelatedQuote = quotesMap.get(newOrder.SBQQ__Quote__c);
                }

Here is my test class:

@isTest
public static void insertOrderTest(){

    Account testAccount = [SELECT Id FROM Account LIMIT 1];
    Opportunity testOpportunity = [SELECT Id FROM Opportunity LIMIT 1];

        //Creating test quote using opportunity and account created above
        SBQQ__Quote__c testQuote = new PS_M3_DataFactory.QuoteCreator(testOpportunity).setLastLineItemNumber(1).setM3OrderNumner('12345').setPricebook(Test.getStandardPricebookId()).setSalesRep(UserInfo.getUserId()).setOffHireDate(system.today()).create();
        testQuote.PO_Number__c = '1234';
        update testQuote;
        //Creating test product and its corresponding pricebook entry
        Product2 testProduct = new Product2(Name='testProduct', Line_Charge_ID__c='lineChargeId', Family='Services', What_is_the_charge_method__c = 'Fixed Rate');
        insert testProduct;
        Product2 testRequiredProduct = new PS_M3_DataFactory.ProductCreator('Required product').create();

        //Creating PricebookEntry
        PricebookEntry testPricebookEntry = new PS_M3_DataFactory.PricebookEntryCreator(Test.getStandardPricebookId()).setProduct(testProduct.Id).setCurrencyIsoCode('USD').create();
        PricebookEntry testRequiredPricebookEntry = new PS_M3_DataFactory.PricebookEntryCreator(Test.getStandardPricebookId()).setProduct(testRequiredProduct.Id).setCurrencyIsoCode('USD').create();

        //Preparing quote lines using test quote, products created above
        Test.startTest();
        SBQQ__QuoteLine__c testQuoteLineItem = new PS_M3_DataFactory.QuoteLineGenerator(testQuote.Id).setNumber(1).setProduct(testProduct.Id).setCurrencyIsoCode('USD').setPrice(100.00).setOffHireDate(Date.today()+1).create();
        SBQQ__QuoteLine__c testRequiredQuoteLineItem = new PS_M3_DataFactory.QuoteLineGenerator(testQuote.Id).setNumber(2).setProduct(testRequiredProduct.Id).setRequiredBy(testQuoteLineItem.Id).setCurrencyIsoCode('USD').setPrice(100.00).create();

        //Order container
        List<Order> orders = new List<Order>();

        //Inserting 5 test orders using data factory classes
        for(Integer i=0; i<5; i++){
            orders.add(new PS_M3_DataFactory.OrderCreator(testAccount.Id).setQuote(testQuote.Id).setCurrencyIsoCode('USD').setPricebook(Test.getStandardPricebookId()).setM3AgreementNumber('12345').setCurrentVersion(true).setCustomerPickupDate(system.today()).setCustomerPickupTime(system.now().time()).setCustomerTimeZone('GMT').setStatus('Draft').create());
        }
        //Invoking trigger handler class by creating instance of the class
        PS_M3_OrderTriggerHandler orderTriggerhandler = new PS_M3_OrderTriggerHandler();

        //Invoking related methods
        orderTriggerhandler.afterUpdate(new Map<Id, Order>(orders),new Map<Id, Order>(orders));

        List<OrderItem> orderItemList = new List<OrderItem>();
        for (Integer i = 0; i < 2; i++) {
        OrderItem oi = new PS_M3_DataFactory.OrderItemCreator(orders[0].Id).setQuoteLine(testQuoteLineItem.Id).setPricebookEntry(testPricebookEntry.Id)
                .setUnitPrice(testQuoteLineItem.SBQQ__CustomerPrice__c).setQuantity(1).getOrderItem();
        oi.SBQQ__QuotedQuantity__c = 1;
        orderItemList.add(oi);
        }
        insert orderItemList;

        List<Order> updatedOrder = new List<Order>();
        updatedOrder.add(orders[0]);

        updatedOrder[0].Status = 'Activated';
        updatedOrder[0].RecordTypeId = Schema.SObjectType.Order.getRecordTypeInfosByName().get('Activated').getRecordTypeId();
        updatedOrder[0].m3Status__c = 'orderActivated';
        update updatedOrder;

        orderTriggerhandler.afterUpdate(new Map<Id, Order>(updatedOrder),new Map<Id, Order>(updatedOrder));
    Test.stopTest();

    //Comparing expected results with actuals
    System.assertEquals([Select count() from Order],5);
  }
}
  • Can you post your test code please? – Carlos Naranjo Feb 24 at 7:10
  • Hi @CarlosNaranjo, added the test class. It's a bit truncated just to show how I am updating orders. Please let me know if I am doing anything wrong or missing something. – Nishi Feb 24 at 7:24
0

The reason for your error is here:

update updatedOrder;

orderTriggerhandler.afterUpdate(new Map<Id, Order>(updatedOrder),new Map<Id, Order>(updatedOrder));

You are passing the Sobject updatedOrder directly to your trigger handler but the fields that SFDC sets once an Order's status changes to Activated aren't in the sobject updatedOrder.

You have to requery like this:

update updatedOrder;
updatedOrder = [SELECT Id, ActivatedById, ... FROM Order 
                  WHERE Id = :updatedOrder.Id] 

orderTriggerhandler.afterUpdate(new Map<Id, Order>(updatedOrder),new Map<Id, Order>(updatedOrder));

Now, having said all this, since you are unit testing a trigger handler, you possibly could have just mocked the whole activated Order using JSON.deserialize and avoided all that test setup. A useful library for mocking objects is @mattaddy's SobjectFabricator

| improve this answer | |
0

There are a few things that you can improve on that test class. You shouldn't use data from your org, instead you should build your on test data. But I think you can take care of that latter.

On the Order record creation I see that you are calling a factory to do so. Can you check if you are inserting these fields on that Order creation execution? Are you inserting a Contract to your process?

Order order = new Order(
            Name='MyNewTestOrder',
            AccountId = MyTestAccount.Id,
            Status='Draft',
            EffectiveDate = Date.today(),
            Opportunity = MyTestOpportunity,
            Pricebook2Id = MyTestPricebook.Id);
insert order; 

Maybe this link can help you to make sure you are following all the steps that you need to reproduce for your test as well.

| improve this answer | |
  • Yes, all these fields are being set in the data factory. No contracts are being created in the process. Thank you for the link. I will try to understand what I am missing in the process – Nishi Feb 24 at 8:12

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.