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
Tommy GeorgiouTommy Georgiou 

test class only covers 50% of the trigger

Hello ,

I have a trigger fro defaulting the standard pricebook for the order
 
trigger InsertPriceBookTrigger on Order (before insert) { 
 List<Pricebook2> stdPBL =  [select id from Pricebook2 where IsStandard = TRUE];
 if(!stdPBL.isEmpty()){
  for(Order o: Trigger.new)
   o.PriceBook2Id = stdPBL[0].id;
  }
}

My test class is
 
@isTest
public class Test5{
    public static testmethod void InsertPriceBookTrigger_Test1()
    {
       Account a = new Account(
            Name = 'Test'
            
        );
        insert a;
        
        Order o = new Order(name='Test1',AccountId=a.id,EffectiveDate=system.today(),status='draft');
        insert o;

    List<Pricebook2> stdPBL =  [select id from Pricebook2 where IsStandard = TRUE];

    if (!stdPBL.isEmpty())
    {
        system.assertequals(stdPBL[0].id,o.PriceBook2Id);
    }
    } 
}

The problem is when I run the test the test class passes but covers only 50% of the trigger. 
Any ideas why??
Best Answer chosen by Tommy Georgiou
Bhanu MaheshBhanu Mahesh
Hi Tommy,

That PriceBook should be available in test class. So use seeAllDate = true
Try below test class
@isTest(seeAllData = true)
public class Test5{
    public static testmethod void InsertPriceBookTrigger_Test1(){
        Account a = new Account(Name = 'Test');
        insert a;
        
        Order o = new Order(name='Test1',AccountId=a.id,EffectiveDate=system.today(),status='draft');
        insert o;
        
        List<Pricebook2> stdPBL =  [select id from Pricebook2 where IsStandard = TRUE];
        
        if (!stdPBL.isEmpty()){
            Order ordr = [select id,PriceBook2Id from Order where id = :o.Id];
            system.assertequals(stdPBL[0].id,ordr.PriceBook2Id);
        }
    } 
}

Regards,
Bhanu Mahesh

All Answers

Bhanu MaheshBhanu Mahesh
Hi Tommy,

That PriceBook should be available in test class. So use seeAllDate = true
Try below test class
@isTest(seeAllData = true)
public class Test5{
    public static testmethod void InsertPriceBookTrigger_Test1(){
        Account a = new Account(Name = 'Test');
        insert a;
        
        Order o = new Order(name='Test1',AccountId=a.id,EffectiveDate=system.today(),status='draft');
        insert o;
        
        List<Pricebook2> stdPBL =  [select id from Pricebook2 where IsStandard = TRUE];
        
        if (!stdPBL.isEmpty()){
            Order ordr = [select id,PriceBook2Id from Order where id = :o.Id];
            system.assertequals(stdPBL[0].id,ordr.PriceBook2Id);
        }
    } 
}

Regards,
Bhanu Mahesh
This was selected as the best answer
Tommy GeorgiouTommy Georgiou
Thank you Bhanu,

That covered the code up to 100%