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
Robert Davis 16Robert Davis 16 

Opportunity Line Item Test Class - Product not being added to Opportunity

I have a trigger that executes on after insert and after create on the Opportunity that updates fields on the OpportunityLineItem Object. Code works but the test class opportunity does not appear to successfully add a product so the code can not go into a IF statement. I am sure this is something simple but dang if I can figure it out. Your help would be greatly appreciated:
 
TRIGGER

//Case Number 1381 - Provides information about request for Trigger
trigger UpdateOTRev on Opportunity (after insert, after update) {
    Set<id> triggerIds = new Set<id>();
    List<OpportunityLineItem> lstOLIUpdate = new List<OpportunityLineItem>();
    //See if Trigger has only been run once
    if(RecursiveTriggerHandler.isFirstTime){
        RecursiveTriggerHandler.isFirstTime = false;
         //Put all trigger opp ids into a set
         for(Opportunity Opps : Trigger.new){
            triggerIds.add(Opps.Id);
         }//END for (Opportunity Opps)
         //Get all the Opp and Products
         List<Opportunity> lstOpps = [SELECT id, CloseDate, (SELECT id, TotalPrice, One_Time_Revenue__c FROM OpportunityLineItems) FROM Opportunity 
                                      WHERE id in: triggerIds];
             //Loop through Opps
             for(Opportunity opp: lstOpps){
                 //Loop through Products in Opps
                 for(OpportunityLineItem oli : opp.OpportunityLineItems){
                     if(oli.One_Time_Revenue__c == true){
                        //Apply the logic of which fields to update
                        if(opp.CloseDate > Date.valueOf('2017-12-31') && opp.CloseDate < Date.valueOf('2018-04-01')){
                            oli.One_Time_Rev_Q1_2018__c = oli.TotalPrice;
                            oli.One_Time_Rev_Q2_2018__c = 0;
                            oli.One_Time_Rev_Q3_2018__c = 0;
                            oli.One_Time_Rev_Q4_2018__c = 0;
                            oli.One_Time_Rev_Q1_2019__c = 0;
                            oli.One_Time_Rev_Q2_2019__c = 0;
                            oli.One_Time_Rev_Q3_2019__c = 0;
                            oli.One_Time_Rev_Q4_2019__c = 0;
                            lstOLIUpdate.add(oli);              
                        }//END if 2018 Q1
                        if(opp.CloseDate > Date.valueOf('2018-03-31') && opp.CloseDate < Date.valueOf('2018-07-01')){
                            oli.One_Time_Rev_Q1_2018__c = 0;
                            oli.One_Time_Rev_Q2_2018__c = oli.TotalPrice;
                            oli.One_Time_Rev_Q3_2018__c = 0;
                            oli.One_Time_Rev_Q4_2018__c = 0;
                            oli.One_Time_Rev_Q1_2019__c = 0;
                            oli.One_Time_Rev_Q2_2019__c = 0;
                            oli.One_Time_Rev_Q3_2019__c = 0;
                            oli.One_Time_Rev_Q4_2019__c = 0;
                            lstOLIUpdate.add(oli);              
                        }//END if 2018 Q2
                        if(opp.CloseDate > Date.valueOf('2018-06-30') && opp.CloseDate < Date.valueOf('2018-10-01')){
                            oli.One_Time_Rev_Q1_2018__c = 0;
                            oli.One_Time_Rev_Q2_2018__c = 0;
                            oli.One_Time_Rev_Q3_2018__c = oli.TotalPrice;
                            oli.One_Time_Rev_Q4_2018__c = 0;
                            oli.One_Time_Rev_Q1_2019__c = 0;
                            oli.One_Time_Rev_Q2_2019__c = 0;
                            oli.One_Time_Rev_Q3_2019__c = 0;
                            oli.One_Time_Rev_Q4_2019__c = 0;
                            lstOLIUpdate.add(oli);              
                        }//END if 2018 Q3
                        if(opp.CloseDate > Date.valueOf('2018-09-30') && opp.CloseDate < Date.valueOf('2019-01-01')){
                            oli.One_Time_Rev_Q1_2018__c = 0;
                            oli.One_Time_Rev_Q2_2018__c = 0;
                            oli.One_Time_Rev_Q3_2018__c = 0;
                            oli.One_Time_Rev_Q4_2018__c = oli.TotalPrice;
                            oli.One_Time_Rev_Q1_2019__c = 0;
                            oli.One_Time_Rev_Q2_2019__c = 0;
                            oli.One_Time_Rev_Q3_2019__c = 0;
                            oli.One_Time_Rev_Q4_2019__c = 0;
                            lstOLIUpdate.add(oli);              
                        }//END if 2018 Q4
                        if(opp.CloseDate > Date.valueOf('2018-12-31') && opp.CloseDate < Date.valueOf('2019-04-01')){
                            oli.One_Time_Rev_Q1_2018__c = 0;
                            oli.One_Time_Rev_Q2_2018__c = 0;
                            oli.One_Time_Rev_Q3_2018__c = 0;
                            oli.One_Time_Rev_Q4_2018__c = 0;
                            oli.One_Time_Rev_Q1_2019__c = oli.TotalPrice;
                            oli.One_Time_Rev_Q2_2019__c = 0;
                            oli.One_Time_Rev_Q3_2019__c = 0;
                            oli.One_Time_Rev_Q4_2019__c = 0;
                            lstOLIUpdate.add(oli);              
                        }//END if 2018 Q1
                        if(opp.CloseDate > Date.valueOf('2019-03-31') && opp.CloseDate < Date.valueOf('2019-07-01')){
                            oli.One_Time_Rev_Q1_2018__c = 0;
                            oli.One_Time_Rev_Q2_2018__c = 0;
                            oli.One_Time_Rev_Q3_2018__c = 0;
                            oli.One_Time_Rev_Q4_2018__c = 0;
                            oli.One_Time_Rev_Q1_2019__c = 0;
                            oli.One_Time_Rev_Q2_2019__c = oli.TotalPrice;
                            oli.One_Time_Rev_Q3_2019__c = 0;
                            oli.One_Time_Rev_Q4_2019__c = 0;
                            lstOLIUpdate.add(oli);              
                        }//END if 2019 Q2
                        if(opp.CloseDate > Date.valueOf('2019-06-30') && opp.CloseDate < Date.valueOf('2019-10-01')){
                            oli.One_Time_Rev_Q1_2018__c = 0;
                            oli.One_Time_Rev_Q2_2018__c = 0;
                            oli.One_Time_Rev_Q3_2018__c = 0;
                            oli.One_Time_Rev_Q4_2018__c = 0;
                            oli.One_Time_Rev_Q1_2019__c = 0;
                            oli.One_Time_Rev_Q2_2019__c = 0;
                            oli.One_Time_Rev_Q3_2019__c = oli.TotalPrice;
                            oli.One_Time_Rev_Q4_2019__c = 0;
                            lstOLIUpdate.add(oli);              
                        }//END if 2019 Q3
                        if(opp.CloseDate > Date.valueOf('2019-09-30') && opp.CloseDate < Date.valueOf('2020-01-01')){
                            oli.One_Time_Rev_Q1_2018__c = 0;
                            oli.One_Time_Rev_Q2_2018__c = 0;
                            oli.One_Time_Rev_Q3_2018__c = 0;
                            oli.One_Time_Rev_Q4_2018__c = 0;
                            oli.One_Time_Rev_Q1_2019__c = 0;
                            oli.One_Time_Rev_Q2_2019__c = 0;
                            oli.One_Time_Rev_Q3_2019__c = 0;
                            oli.One_Time_Rev_Q4_2019__c = oli.TotalPrice;
                            lstOLIUpdate.add(oli);              
                        }//END if 2019 Q4
                        if(opp.CloseDate > Date.valueOf('2019-12-31')){
                            oli.One_Time_Rev_Q1_2018__c = 0;
                            oli.One_Time_Rev_Q2_2018__c = 0;
                            oli.One_Time_Rev_Q3_2018__c = 0;
                            oli.One_Time_Rev_Q4_2018__c = 0;
                            oli.One_Time_Rev_Q1_2019__c = 0;
                            oli.One_Time_Rev_Q2_2019__c = 0;
                            oli.One_Time_Rev_Q3_2019__c = 0;
                            oli.One_Time_Rev_Q4_2019__c = 0;
                            
                            lstOLIUpdate.add(oli);
                        }
                         
                     }//END if(oli.One_Time_Revenue__c == true)                     
                 }//END For(OpportunityLineItem)
             }//END for(Opportunity)
    //Update Products if there are any to update
    if(lstOLIUpdate.size()>0){
        update lstOLIUpdate;
    }
    }//END if(RecusiveTriggerHandler.isFirstTime)
}//END Class

TEST CLASS
@isTest
public class UpdateOTRevTest {
    
    public static testMethod void testOppOneTimeRevenue(){
        
        Product2 prod2= new Product2(Name = 'Test Product - OneTime',
                                     Product_Gross_Margin__c = 10,
                                     Delivery_Market__c = 'none' ,
                                     Delivery_Region__c = 'ABCD',
                                     Delivery_Site__c = 'none' ,
                                     Family = 'XTCD',
                                     Description = 'Test Product',
                                     IsActive = true,
                                     Project_Seat_Count__c = 'false',
                                     One_Time_Revenue__c = true);
        insert prod2;
        Id pricebookId = Test.getStandardPricebookId();
        PricebookEntry pbEntry = new PricebookEntry(Pricebook2Id = pricebookId,
                                                    product2Id = prod2.Id,
                                                    isActive = true,
                                                    UnitPrice = 1000);
        insert pbEntry;
       
        Opportunity opp = new Opportunity(Name = 'Test Account - Opportunity',
                                          StageName = '1 - Prospect',
                                          Type = 'New Logo', 
                                          CloseDate = System.Date.today(),
                                          LeadSource = 'BDD/AM/OPS Development',
                                          Referral_Channel__c = 'None',
                                          Auto_Generate_Quarterly_Forecast__c = 'Yes',
                                          Contract_Length_Months__c = 24,
                                          Pricebook2Id = pricebookId,
                                          Amount = 10000000);
        insert opp;
       
        OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.id,
                                                          PricebookEntryId = pbEntry.id,
                                                          Product2Id = prod2.id,
                                                          Quantity = 10, 
                                                          Unit_Price2__c = 20,
                                                          Adjusted_Gross_Margin_Percentage__c = 10,
                                                          Service_Channel__c = 'WIDGET',
                                                          Service_Type__c = 'Care',
                                                          One_Time_Revenue__c = true);
        insert oli;
        
        System.debug('Opportunity Product Total Price '+ oli.TotalPrice);
        System.debug('OpportunityLineItem Id ' +oli.Id);
        System.debug('Product 2 Id '+ oli.Product2Id);
        System.debug('Opportunity Line Item Name : '+ oli.Name);
        System.debug('One Time Revenue : '+oli.One_Time_Revenue__c);
        Date closeDate = System.today();
        opp.CloseDate = Date.newInstance(2018, 01, 25);
       
        update opp;
        System.debug('Opp Close Date : '+opp.CloseDate);
    }//END Method
}//END Class

//https://salesforce.stackexchange.com/questions/72883/how-to-create-opportunity-line-items-in-test-classes

Code coverage is 10 of 103 lines because it does not appear that the OpportunityLineItem ever gets associated with the Opportunity.
User-added image

I would greatly appreciate any help.

Thank you. ​
Best Answer chosen by Robert Davis 16
Robert Davis 16Robert Davis 16
This issue was the recursion class that was preventing the recursion in the class. What I ended up doing is before the Update portion of the trigger I set the RecursiveTriggerHandler.isFirstTime = true; This fixed the issue.

All Answers

Gururaj BGururaj B
I think the test execution that is shown is for the insert statment of the opporutnity which is done in your test script before adding line item(its obvious line item can be added only after opporutnity). Try removing the (after insert) argument on the trigger and keep only (After update) and retest.
Robert Davis 16Robert Davis 16
Gururaj,

Thanks for the reply.

I removed the after insert from the trigger but this did not affect the test results.

Robert
Robert Davis 16Robert Davis 16
When I put the System.Debug('Opportunity HasLineItem '+ opp.hasLineItem) I get a value of false.
 
Robert Davis 16Robert Davis 16
This issue was the recursion class that was preventing the recursion in the class. What I ended up doing is before the Update portion of the trigger I set the RecursiveTriggerHandler.isFirstTime = true; This fixed the issue.
This was selected as the best answer
radha gorintaradha gorinta
After every insert in the test class you have to  query the required values, because your variables don't automatically update to their latest values

ex:  insert opp;  
    Opportunity insertedopportunity=[select Id,Pricebook2Id from Opportunity where id=:opp.id];

same thing  for variables  prod2 and pbentry also

OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = insertedopportunity.id,...................