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
Michael Hedrick 2Michael Hedrick 2 

Apex test class for oldmap

Hello
I am having trouble increading my COde coverage for my Trigger with my Test Class. 
@isTest (SeeAllData = false)
public class TestNewEarlyBuySalesProgram {
    @isTest    
    public static void myTestClass() 
    {
        Account a = new Account();
            a.Name= 'Test SP Account';
            a.Type= 'Dealer / Distributor';
            a.Partner_Type__c = 'Dealer';
            a.Tier__c = 'Stocking Dealer';
            a.Status__c = 'Active';     
            a.RecordTypeId = '012j0000000cfvw';
         insert a;

        Sales_Program__c sp = new Sales_Program__c();
            sp.Name = 'TestSP';
            sp.Record_Processed__c = False;
            sp.JDE_Number__c = '11111';
            sp.Account__c = a.id;
            sp.Current_SBF_Year__c='2016';
            sp.sales_Program_Type__c = 'SBF';
            sp.External_Id__c = '2016_11111';
            sp.Trex_Brand_Exclusive_Percentage__c= 1.0;
            sp.Trex_1_Competitive_Brand_Percentage__c = 2.0;
            sp.Early_Buy_PY_Total__c=0;
            sp.Trex_Exclusive__c = True;
            sp.Trex_1_Competitive_Brand__c = False;
           
        insert sp;     
        
        sp.Early_Buy_PY_Total__c=3456874;
        sp.Record_Processed__c = True;
        update sp; 

        test.startTest();
     
      //   sp.Early_Buy_PY_Total__c=3456874;
       //  sp.Record_Processed__c = True;
      //   update sp;
            
            Sales_Program__c sp1 = new Sales_Program__c();
            sp1.Name = 'TestSP1';
            sp1.Record_Processed__c = false;
            sp1.JDE_Number__c = '11111';
            sp1.Account__c = a.id;
            sp1.Current_SBF_Year__c='2017';
            sp1.sales_Program_Type__c = 'SBF';
            sp1.External_Id__c = '2017_11111';
            sp1.Trex_Brand_Exclusive_Percentage__c= 1.0;
            sp1.Trex_1_Competitive_Brand_Percentage__c = 2.0;
            sp1.Prior_Year_Indirect_Sales__c = 3456874;
            sp1.Trex_Exclusive__c = True;
            sp1.Trex_1_Competitive_Brand__c = False;
     //   update sp1;     
        
      //  test.startTest();
           insert sp1 ; 
        test.stopTest();          
       
    }

}

I have added the trigger below.  The trigger created a new record when a field(Early_Buy_PY_Total) on the original object is update and the field Record_Processed__c = false.  A workflow updates this field to ensure it does not get processes more than once.  Coverage is at a dismall 26% but I am guessing that is because the Test class is not triggering the Apex Trigger.  Any suggestions would be greatl;y appreciated.
Thanks


User-added image
 
Best Answer chosen by Michael Hedrick 2
Amit Chaudhary 8Amit Chaudhary 8
Please deactivate the workflow.
try below code
trigger SalesProgramNewTrigger on Sales_Program__c (before update) 
{
	List<Sales_Program__c> spnew=new List<Sales_Program__c>();
	
    for(Sales_Program__c spold : Trigger.New)
    {
	
		if(spold.Early_Buy_PY_Total__c != Trigger.oldMap.get(spold.id).Early_Buy_PY_Total__c && spold.Record_Processed__c != True && spold.Early_Buy_PY_Total__c  > 0 )
		{     
			  Integer newDate = Integer.valueOf(spold.Current_SBF_Year__c) +1; 
			  string snewdate = string.valueOf(newDate);
			  Sales_Program__c s = new Sales_Program__c();
			  s.sales_Program_Type__c =spold.Sales_Program_Type__c;
			  s.Account__c = spold.Account__c;
			  s.External_Id__c = snewDate+'_'+ spold.JDE_Number__c;
			  s.Account_Contacts__c = spold.Account_Contacts__c;  
			  s.JDE_Number__c = spold.JDE_Number__c;
			  s.Prior_Year_Indirect_Sales__c= spold.Early_Buy_PY_Total__c ;  
			  s.Trex_Exclusive__c= spold.Trex_Exclusive__c;
			  S.Trex_1_Competitive_Brand__c= spold.Trex_1_Competitive_Brand__c;
			  s.Current_SBF_Year__c = snewDate;
			  s.Test_Value_for_Calculate_EB_Growth__c = spold.Test_Value_for_Calculate_EB_Growth__c;
			  spnew.add(s);
			  
				spold.Record_Processed__c = true; // Set True here only
		}
    }   
	
		if(spnew.size()>0)
			insert spnew;
}
And let us know if this will help you
 

All Answers

ZX CheowZX Cheow
Hi Michael,

Since the trigger is to create a new record after the existing record is updated to fulfill a certain criteria, the test should be run on the update action instead as below.

test.startTest();
sp.Early_Buy_PY_Total__c=3456874;
sp.Record_Processed__c = True;
update sp;
test.stopTest(); 

The test data creation of SP1 is not necessary as the creation of SP1 is what you're expecting from the trigger to perform and to be tested.

Thanks,
ZX

If this helps, please marks as best answer. :)
Amit Chaudhary 8Amit Chaudhary 8
Please update your test class like below
@isTest
public class TestNewEarlyBuySalesProgram 
{
    @isTest    
    public static void myTestClass() 
    {
        Account a = new Account();
            a.Name= 'Test SP Account';
            a.Type= 'Dealer / Distributor';
            a.Partner_Type__c = 'Dealer';
            a.Tier__c = 'Stocking Dealer';
            a.Status__c = 'Active';     
            a.RecordTypeId = '012j0000000cfvw';
         insert a;

        Sales_Program__c sp = new Sales_Program__c();
            sp.Name = 'TestSP';
            sp.Record_Processed__c = False;
            sp.JDE_Number__c = '11111';
            sp.Account__c = a.id;
            sp.Current_SBF_Year__c='2016';
            sp.sales_Program_Type__c = 'SBF';
            sp.External_Id__c = '2016_11111';
            sp.Trex_Brand_Exclusive_Percentage__c= 1.0;
            sp.Trex_1_Competitive_Brand_Percentage__c = 2.0;
            sp.Early_Buy_PY_Total__c=0;
            sp.Trex_Exclusive__c = True;
            sp.Trex_1_Competitive_Brand__c = False;
        insert sp;     
        

        test.startTest();

			sp.Early_Buy_PY_Total__c=3456874;
			sp.Record_Processed__c = false;
			update sp;
		
        test.stopTest();          
       
    }

}
Let us know if this will help you

 
Michael Hedrick 2Michael Hedrick 2
Ok.  I recieved an error.  The issue is that I have a workflow that updates the  Record_Processed__c to True once the Early_Buy_PY_Total__c field is populated.  The reason for this is that I did not want the trigger to run again if the Early_Buy_PY_Total__c field is updated.  Can I have the trigger update the Record_Processed__c on the original record to True after I have created the new rwecord?
Thanks for your help and time...
Amit Chaudhary 8Amit Chaudhary 8
Please post your trigger code. You need to change your code event from After Update to before update and update Record_Processed__c  field in same trigger and remove the workflow.
Michael Hedrick 2Michael Hedrick 2
Here is the trigger as it stands today.
trigger SalesProgramNewTrigger on Sales_Program__c (after update) 
{
 List<Sales_Program__c> spnew=new List<Sales_Program__c>();
     for(Sales_Program__c spold : Trigger.New)
     {
           if(spold.Early_Buy_PY_Total__c != Trigger.oldMap.get(spold.id).Early_Buy_PY_Total__c && spold.Record_Processed__c != True && spold.Early_Buy_PY_Total__c  > 0 )
           {     
                  Integer newDate = Integer.valueOf(spold.Current_SBF_Year__c) +1; 
                  string snewdate = string.valueOf(newDate);
                  Sales_Program__c s = new Sales_Program__c();
                  s.sales_Program_Type__c =spold.Sales_Program_Type__c;
                  s.Account__c = spold.Account__c;
                  s.External_Id__c = snewDate+'_'+ spold.JDE_Number__c;
                  s.Account_Contacts__c = spold.Account_Contacts__c;  
                  s.JDE_Number__c = spold.JDE_Number__c;
                  s.Prior_Year_Indirect_Sales__c= spold.Early_Buy_PY_Total__c ;  
                  s.Trex_Exclusive__c= spold.Trex_Exclusive__c;
                  S.Trex_1_Competitive_Brand__c= spold.Trex_1_Competitive_Brand__c;
                  s.Current_SBF_Year__c = snewDate;
                  s.Test_Value_for_Calculate_EB_Growth__c = spold.Test_Value_for_Calculate_EB_Growth__c;
                  spnew.add(s);
              
            }
          //  if(spnew.size()>0)
        //    insert spnew;
     }   
    
                if(spnew.size()>0)
            insert spnew;
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please deactivate the workflow.
try below code
trigger SalesProgramNewTrigger on Sales_Program__c (before update) 
{
	List<Sales_Program__c> spnew=new List<Sales_Program__c>();
	
    for(Sales_Program__c spold : Trigger.New)
    {
	
		if(spold.Early_Buy_PY_Total__c != Trigger.oldMap.get(spold.id).Early_Buy_PY_Total__c && spold.Record_Processed__c != True && spold.Early_Buy_PY_Total__c  > 0 )
		{     
			  Integer newDate = Integer.valueOf(spold.Current_SBF_Year__c) +1; 
			  string snewdate = string.valueOf(newDate);
			  Sales_Program__c s = new Sales_Program__c();
			  s.sales_Program_Type__c =spold.Sales_Program_Type__c;
			  s.Account__c = spold.Account__c;
			  s.External_Id__c = snewDate+'_'+ spold.JDE_Number__c;
			  s.Account_Contacts__c = spold.Account_Contacts__c;  
			  s.JDE_Number__c = spold.JDE_Number__c;
			  s.Prior_Year_Indirect_Sales__c= spold.Early_Buy_PY_Total__c ;  
			  s.Trex_Exclusive__c= spold.Trex_Exclusive__c;
			  S.Trex_1_Competitive_Brand__c= spold.Trex_1_Competitive_Brand__c;
			  s.Current_SBF_Year__c = snewDate;
			  s.Test_Value_for_Calculate_EB_Growth__c = spold.Test_Value_for_Calculate_EB_Growth__c;
			  spnew.add(s);
			  
				spold.Record_Processed__c = true; // Set True here only
		}
    }   
	
		if(spnew.size()>0)
			insert spnew;
}
And let us know if this will help you
 
This was selected as the best answer
Michael Hedrick 2Michael Hedrick 2
Perfect! Thanks to you both for your help.
Michael Hedrick 2Michael Hedrick 2
And 100% coverage.....