+ Start a Discussion
Hermann OuréHermann Ouré 

Test for Apex trigger on Order object not covering code (coverage 0%)

Hello,

I have created a trigger on the Order object that works fine. But I am not able to get any coverage of my trigger with my test class.
Even though I wrote a test class; the coverage of my trigger remains at 0%
Could someone tell me what am I doing wrong?
Thanks

Trigger:
 
trigger UpdateAccountCA on Order (after update) {
	
    Map<Id, Decimal> mAccAmount = new Map<Id, Decimal>();
    
    //Iterate through each Order 
    for(Order o : Trigger.new) {
        if(mAccAmount.containskey(o.accountid)) {
          Decimal d = mAccAmount.get( o.AccountId );
          System.debug('Decimal value ' +d);
          d += o.TotalAmount;

          mAccAmount.put( o.AccountId, d );  
            
        } else {
            mAccAmount.put( o.AccountId, o.TotalAmount );

        }
        
    }
    List<Account> lAccs = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :mAccAmount.keySet()];
	System.debug('Account Keyset: ' +mAccAmount.keySet());
    
    //Iterate through each List of Accounts 
    if(lAccs.size()>0) {
        for(Account acc : lAccs){
            acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + mAccAmount.get(acc.Id);  
        }
    }
    update lAccs;  
}

Test class:
 
@isTest
public class UpdateAccountCATest {
    
    @isTest static void test() {
        Account acc1 = new Account(Name = 'Test Account 1');
        insert acc1;
      
        Id pricebookId = Test.getStandardPricebookId();
        
        Product2 pd1 = new Product2(Name = 'Chemise Verte longue XYX', Family = 'Chemise');
        Insert pd1;
 
        //Create the PricebookEntry
        PricebookEntry standardPrice = new PricebookEntry();
            standardPrice.Pricebook2Id = pricebookId;
            standardPrice.Product2Id = pd1.Id;
            standardPrice.UnitPrice = 1020;
            standardPrice.IsActive = true;
        	standardPrice.UseStandardPrice = false;
        
        Insert standardPrice;

        Order o1 = new Order(AccountId = acc1.Id, EffectiveDate=date.today(), Status='Draft', Pricebook2Id = pricebookId);
        insert o1;

        OrderItem oi1 = new OrderItem (OrderId = o1.Id, PricebookEntryId = standardPrice.Id, Quantity=10, UnitPrice = 150);
        insert oi1;
        OrderItem oi2 = new OrderItem (OrderId = o1.Id, PricebookEntryId = standardPrice.Id, Quantity=20, UnitPrice = 1000);
        insert oi2;        
    }
 
}

 
louisa barrett 7louisa barrett 7
Hi,

Your trigger is only set as an after update 'trigger UpdateAccountCA on Order (after update)' but on your test class you are only inserting orders never updating them. You would need to insert the order and then update them in order for your trigger to fire or adjust your trigger so it fired on an insert as well, so add 'after insert' to the trigger events
Hermann OuréHermann Ouré
Hello Louisa,
Thanks to you I managed to get 66% coverage. 
The only thing, If you can help with is that now, when running the test I have an error:
System.NullPointerException: Attempt to de-reference a null object
 
//Iterate through each List of Accounts 
    if(lAccs.size()>0) {
        for(Account acc : lAccs){
            acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + mAccAmount.get(acc.Id);  
        }
    }
    update lAccs;

 
louisa barrett 7louisa barrett 7
What have you changed on the trigger, did you add 'after insert' to the trigger events?
Can you send the full code through and the line number for the error
Hermann OuréHermann Ouré
here is the full code:
 
trigger UpdateAccountCA on Order (after update, after insert) {
	
    Map<Id, Decimal> mAccAmount = new Map<Id, Decimal>();
    
    //Iterate through each Order 
    for(Order o : Trigger.new) {
        if(mAccAmount.containskey(o.accountid)) {
          Decimal d = mAccAmount.get( o.AccountId );
          System.debug('Decimal value ' +d);
          d += o.TotalAmount;

          mAccAmount.put( o.AccountId, d );  
            
        } else {
            mAccAmount.put( o.AccountId, o.TotalAmount );

        }
        
    }
    List<Account> lAccs = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :mAccAmount.keySet()];
	System.debug('Account Keyset: ' +mAccAmount.keySet());
    
    //Iterate through each List of Accounts 
    if(lAccs.size()>0) {
        for(Account acc : lAccs){
            acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + mAccAmount.get(acc.Id);  
        }
    }
    update lAccs;  
}

 
louisa barrett 7louisa barrett 7
Can you send the revised test class and the full error with line number
Hermann OuréHermann Ouré
line number for the error:
caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.UpdateAccountCA: line 26, column 1: []
Hermann OuréHermann Ouré
ok 

Trigger:
 
trigger UpdateAccountCA on Order (after insert, after update) {
	
    Map<Id, Decimal> mAccAmount = new Map<Id, Decimal>();
    
    //Iterate through each Order 
    for(Order o : Trigger.new) {
        if(mAccAmount.containskey(o.accountid)) {
          Decimal d = mAccAmount.get( o.AccountId );
          System.debug('Decimal value ' +d);
          d += o.TotalAmount;

          mAccAmount.put( o.AccountId, d );  
            
        } else {
            mAccAmount.put( o.AccountId, o.TotalAmount );

        }
        
    }
    List<Account> lAccs = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :mAccAmount.keySet()];
	System.debug('Account Keyset: ' +mAccAmount.keySet());
    
    //Iterate through each List of Accounts 
    if(lAccs.size()>0) {
        for(Account acc : lAccs){
            acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + mAccAmount.get(acc.Id);  
        }
    }
    update lAccs;  
}

Test Class:
 
@isTest
public class UpdateAccountCATest {
    
    @isTest static void test() {
        Account acc1 = new Account(Name = 'Test Account 1');
        insert acc1;
      
        Id pricebookId = Test.getStandardPricebookId();
        
        Product2 pd1 = new Product2(Name = 'Chemise Verte longue XYX', Family = 'Chemise');
        Insert pd1;
 
        //Create the PricebookEntry
        PricebookEntry standardPrice = new PricebookEntry();
            standardPrice.Pricebook2Id = pricebookId;
            standardPrice.Product2Id = pd1.Id;
            standardPrice.UnitPrice = 1020;
            standardPrice.IsActive = true;
        	standardPrice.UseStandardPrice = false;
        
        Insert standardPrice;

        Order o1 = new Order(AccountId = acc1.Id, EffectiveDate=date.today(), Status='Draft', Pricebook2Id = pricebookId);
        insert o1;

        OrderItem oi1 = new OrderItem (OrderId = o1.Id, PricebookEntryId = standardPrice.Id, Quantity=10, UnitPrice = 150);
        insert oi1;
        OrderItem oi2 = new OrderItem (OrderId = o1.Id, PricebookEntryId = standardPrice.Id, Quantity=20, UnitPrice = 1000);
        insert oi2;        
    }
 
}

Error:
System.NullPointerException: Attempt to de-reference a null object
Trigger.UpdateAccountCA: line 26, column 1: []
louisa barrett 7louisa barrett 7
I've just run your code in a sandbox and it runs without error, it only gets 75% coverage as you are only creating one account so the if statement on line 7 of your trigger is never going to get run.
I would suggest you look at the trigger again though, as there is no need to to have it as an insert if the amount field is populated from the order line items. And the field that you are updating is only ever going to get added to, you are not overwriting it each time, is that correct?
Hermann OuréHermann Ouré
Yes, that's correct.
Thanks, I'll review my code see what it shows an error in my side.