0

Can someone please help me how can I get the code coverage for below line:

enter image description here

Test Class Code

 static testMethod void OpportunityScheduleHandler() {
         
            Date Closedt = Date.valueOf('2021-12-05');
            Date SOPdt = Date.valueOf('2021-01-01');
            Date Schdt = Date.valueOf('2021-01-02');
            Date EXpdt = Date.valueOf('2021-01-03');
            Date servicedate = Date.valueOf('2021-12-05');
         
            Account acc = new Account();
            acc.Name = 'account1';
            insert acc;
         
            /*Insert Product*/
            Product2 Prod =  new Product2();
            Prod.Name='NewProd';
            Prod.Product_Type__c = 'Royalty';
            prod.CanUseQuantitySchedule = true;
            prod.CanUseRevenueSchedule = true;
            Prod.IsActive=True;
            insert Prod;
            
            /*Insert Pricebook2*/
            Pricebook2 standardPricebook = new Pricebook2(
            Id = Test.getStandardPricebookId(),
            IsActive = true
            );
            update standardPricebook;
            /*Insert PricebookEntry*/
            PricebookEntry pbe = new PricebookEntry();
            pbe.Product2Id=Prod.Id;
            pbe.IsActive=True;
            pbe.UnitPrice=70;
            pbe.Pricebook2Id = standardPricebook.Id;
          //  pbe.UseStandardPrice=false;
            insert pbe;
         /*Insert Opportunity*/
            Opportunity opp = new Opportunity();
            opp.AccountId = acc.Id;
            opp.CurrencyIsoCode='EUR';
            opp.Name = 'Test';
            opp.StageName = 'ClosedWon';
            opp.CloseDate= Closedt;
            opp.SOP_Date__c = SOPdt;
            insert opp;
            opp.SOP_Date__c = date.newInstance(2021, 12, 17);
            update opp;             
           
            /*Insert OpportunityLineItem*/
            OpportunityLineItem opli = new OpportunityLineItem();
            opli.UnitPrice = 57;
            opli.Quantity = 12;
            opli.OpportunityId=opp.Id;
            opli.PricebookEntryId=pbe.id;
            insert opli;
            
         
            /*Insert OpportunityLineItemSchedule*/
            OpportunityLineItemSchedule oplis = new OpportunityLineItemSchedule();
            oplis.Quantity = 0;
            oplis.OpportunityLineItemId=opli.Id;
            oplis.Type='Quantity';
            opli.ServiceDate=servicedate;
            oplis.ScheduleDate=Schdt;
            insert oplis;
             
            Integer DayDiff = 1;
            //Integer DayDiff =  oldOppr.daysBetween(newOppr);
             
           if(oplis.Quantity == 0)
            {
            oplis.ScheduleDate=oplis.ScheduleDate+DayDiff;
            }    
            else{
               oplis.ScheduleDate=oplis.ScheduleDate ;
           }
         
            System.assertEquals( oplis.ScheduleDate, EXpdt);
 
}    

Class Code

public class OpportunitySchedulingHandler {
     public static void ScheduleDateUpdate(String opprid, Integer DayDiff) 
    {
       
        
        
       List <OpportunityLineItem> oli = [SELECT id,OpportunityId,ProductType__c,
                                              (SELECT id ,ScheduleDate,Quantity
                                              FROM OpportunityLineItemSchedules)  
                                              FROM OpportunityLineItem 
                                              WHERE ProductType__c = 'Royalty' and OpportunityId =:opprid];
        OpportunityLineItemSchedule[] opportunityupdates = new OpportunityLineItemSchedule[] {};
            
        for(OpportunityLineItem oppLI:oli){
            for(OpportunityLineItemSchedule olis:oppLI.OpportunityLineItemSchedules){
               if(olis.Quantity == 0){
                    date addedservicedate2;
                    addedservicedate2 = olis.ScheduleDate+(DayDiff);
                    olis.ScheduleDate = addedservicedate2;
                    opportunityupdates.add(olis);
                    system.debug('olis.ScheduleDate'+olis.ScheduleDate);
            }  
            }
        }
        update opportunityupdates;
        }
  
    }
1
  • 1
    It appears to me the loop isn't entered because there are no OpportunityLineItemSchedules to iterate over. I don't see a line in your test class that calls ScheduleDateUpdate explicitly, is it called via a trigger?
    – nbrown
    May 24 at 12:46
0

I believe it is an issue of the sequence of transactions. If ScheduleDateUpdate method is getting called from opportunity trigger than your oli records would be created post this method execution.

So - you need to create test data first, and then update the opportunity to trigger ScheduleDateUpdate method with proper data.

If this does not work, provide your trigger code.

Not the answer you're looking for? Browse other questions tagged or ask your own question.