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.
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 (Never enters the If Statement):
@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
Test Coverage:
It is almost as if the test class does not recognize that there are products on the Opportunity, but there is a OpportunityId on the OpportunityLineItem Record.