• radha gorinta
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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. ​
My requirement is incremnet of "No_of_JobApps__c" when i inserted/Undeleted Job_Application__c related to a Position__c and decrement of No_of_JobApps__c when i deleted Job_Application__c related to a Position__c...
1. No_of_JobApps__c is a number data type on Position__c custom object
2.Job_Application__c is related with Position__c(Master object). code is fine but i am not getting No_of_JobApps__c field updated.
Trigger code:
trigger countJobApp on Job_Application__c (after insert, after delete, after undelete) {
    if(trigger.isAfter){
    set<id> jobAppIds= new set<id>();
        if(trigger.isInsert || trigger.isUnDelete){
            for(Job_Application__c jobs:trigger.new){
                jobAppIds.add(jobs.Position__r.id);  
            }
        }
        if(trigger.isDelete){
            for(Job_Application__c jobs:trigger.old){
                jobAppIds.add(jobs.Position__r.id);
            }
            
        }
    list<Position__c> pos= new list<Position__c>();
    list<Position__c> jobsforPos=[SELECT No_of_JobApps__c, id, (SELECT id FROM Job_Applications__r) FROM Position__c where ID IN :jobAppIds];
        for(Position__c jobPos:jobsforPos){
            integer count=0;
            for(Job_Application__c japps:jobPos.Job_Applications__r){
                count++;
            }
            if(jobPos.No_of_JobApps__c !=count){
                jobPos.No_of_JobApps__c = count;
                pos.add(jobPos);
            }
        }
        if(pos.size()>0){
            update pos;
        }
    }
}