You need to sign in to do that
Don't have an account?
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:
Test class:
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; } }
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
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
Can you send the full code through and the line number for the error
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.UpdateAccountCA: line 26, column 1: []
Trigger:
Test Class:
Error:
System.NullPointerException: Attempt to de-reference a null object
Trigger.UpdateAccountCA: line 26, column 1: []
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?
Thanks, I'll review my code see what it shows an error in my side.