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
Theodosia TeniaTheodosia Tenia 

How to get apex class and trigger coverage for revenue schedule elements?

I added the following apex classes and triggers to our Sandbox enviroment that worked fine but when I try to deploy it in production I got an error that apex classes need 75% coverge and triggerse need at least 1%.  Both are currently at 0%. How do I add code coverge in order to delpoy these?  Both classes and the trigger are included below:

SetRevenueSchedule
trigger setRevenueSchedule on OpportunityLineItem (after insert, after update) {
    if(RecursionBlocker.flag){
                
        if (trigger.isAfter && trigger.isInsert){
            RevenueController.insertRevenue(trigger.new);
        }   
        
        if (trigger.isAfter && trigger.isUpdate){
            RevenueController.updateRevenue(trigger.newMap, trigger.oldMap);
        }     
    }//end of if recursionblocker is true.
    
}


RecursionBlocker
public with sharing class RecursionBlocker {
  public static Boolean flag = true;
}

RevenueController
public class RevenueController
{   
    public static void insertRevenue(List<OpportunityLineItem> LI){
        List<OpportunityLineItemSchedule> oppLS = new List<OpportunityLineItemSchedule>();
        
        Date ServiceDate;
        Date EndDate;

        Decimal monthlyRevenue = 0, TotalPrice = 0, UnitPrice = 0;
    
        Integer NumberOfRevenueInstallments, numberOfDays;
        String RevenueInstallmentPeriod = 'Daily';
        String RevenueScheduleType = 'Divide';
               
        List<Id> oliIds = new List<Id>();
        for (OpportunityLineItem olit : LI){
            oliIds.add(olit.Id);
        }
        
        for (OpportunityLineItem item : [SELECT Id, ServiceDate, End_Date__c, UnitPrice, TotalPrice, PricebookEntryId FROM OpportunityLineItem WHERE Id IN :oliIds]) {
            
            ServiceDate = item.ServiceDate;
            EndDate = item.End_Date__c;
            TotalPrice = item.TotalPrice;
            UnitPrice = item.UnitPrice;
                    
            NumberOfRevenueInstallments = (((EndDate.year() * 12) + EndDate.MONTH()) - ((ServiceDate.year() * 12) + ServiceDate.month()));
            numberOfDays = ServiceDate.daysBetween(EndDate) + 1;
            
            monthlyRevenue = (TotalPrice / numberOfDays);
            
            Decimal computedRevenue = 0;
            for(Integer i=0; i<numberOfDays; i++){
                Date newDate = ServiceDate.addDays(i);
                computedRevenue = computedRevenue + monthlyRevenue;
                if(i == numberOfDays && (computedRevenue < TotalPrice || computedRevenue > TotalPrice)){
                    monthlyRevenue = (monthlyRevenue + (TotalPrice - computedRevenue)).abs();
                }
                if(i == numberOfDays && newDate > EndDate){
                    newDate = EndDate;
                }

            OpportunityLineItemSchedule opS = new OpportunityLineItemSchedule(OpportunityLineItemId = item.Id, Type='Revenue', Revenue = monthlyRevenue, ScheduleDate = newDate);
                oppLS.add(opS);
                                
            }
        }
                        
        RecursionBlocker.flag = false;
                                                
        try{
            insert oppLS;
            system.debug('#Success from insert :');
        } catch(system.DmlException e){
            system.debug('##Error OpportunityLineItemSchedule : ' + e.getMessage()); 
        }
    }   
        
    public static void updateRevenue(map<Id, OpportunityLineItem> newOppsMap, map<Id, OpportunityLineItem> oldOppsMap){
        
        List<OpportunityLineItemSchedule> oppLS = new List<OpportunityLineItemSchedule>();
        List<OpportunityLineItemSchedule> oldoppLS = new List<OpportunityLineItemSchedule>();
    
        list<OpportunityLineItem> newOppLiList = new list<OpportunityLineItem>();
        
        Date ServiceDate;
        Date EndDate;
        
        Decimal Revenue = 0, TotalPrice = 0;
    
        Integer NumberOfRevenueInstallments, numberOfDays;
        String RevenueInstallmentPeriod = 'Daily';
        String RevenueScheduleType = 'Divide';
        
        Set<Id> opportunityLineItemIds = new Set<Id>();
        Set<Id> newId = new Set<Id>();
        
        for(Id liId :newOppsMap.keySet()){
            opportunityLineItemIds.add(newOppsMap.get(liId).Id);
            OpportunityLineItem temp = new OpportunityLineItem();
            
            if((newOppsMap.get(liId).ServiceDate != oldOppsMap.get(liId).ServiceDate) || (newOppsMap.get(liId).End_Date__c != oldOppsMap.get(liId).End_Date__c)){
                newId.add(newOppsMap.get(liId).Id);
            }

            temp.OpportunityId = newOppsMap.get(liId).OpportunityId;
            newOppLiList.add(temp);
        }
        
        if(newId.size() > 0){
            for(OpportunityLineItemSchedule ps : [SELECT Id, OpportunityLineItemId From OpportunityLineItemSchedule Where OpportunityLineItemId IN :opportunityLineItemIds]){
                OpportunityLineItemSchedule tempS = new OpportunityLineItemSchedule();
                tempS.Id = ps.Id;
                oldoppLS.add(tempS);
            }
            
            for (OpportunityLineItem item : [SELECT Id, ServiceDate, End_Date__c, UnitPrice, TotalPrice, PricebookEntryId FROM OpportunityLineItem WHERE Id IN :opportunityLineItemIds]) {
                
                ServiceDate = item.ServiceDate;
                EndDate = item.End_Date__c;
                TotalPrice = item.UnitPrice;
                        
                NumberOfRevenueInstallments = (((EndDate.year() * 12) + EndDate.MONTH()) - ((ServiceDate.year() * 12) + ServiceDate.month()));
                numberOfDays = ServiceDate.daysBetween(EndDate);
                                
                Revenue = (TotalPrice / numberOfDays);
                
                Decimal computedRevenue = 0;
                for(Integer i=0; i<numberOfDays; i++){
                    Date newDate = ServiceDate.addDays(i);
                    computedRevenue = computedRevenue + Revenue;
                    if(i == numberOfDays && (computedRevenue < TotalPrice || computedRevenue > TotalPrice)){
                        Revenue = (Revenue + (TotalPrice - computedRevenue)).abs();
                    }
                    if(i == numberOfDays && newDate > EndDate){
                        newDate = EndDate;
                    }

                OpportunityLineItemSchedule opS = new OpportunityLineItemSchedule(OpportunityLineItemId = item.Id, Type='Revenue', Revenue = Revenue, ScheduleDate = newDate);
                    oppLS.add(opS);
                                    
                }
            }
                            
            RecursionBlocker.flag = false;
                                                            
            try{
                insert oppLS;
                system.debug('#Success insert oppLS : ' + oppLS.size());
            } catch(system.DmlException e){
                system.debug('##Error OpportunityLineItemSchedule : ' + e.getMessage()); 
            }
            
            try{
                delete oldoppLS;
                system.debug('#Success Delete oppLS');
            } catch(system.DmlException e){
                system.debug('##Error OpportunityLineItemSchedule : ' + e.getMessage()); 
            }   
        }
    }
}

Thanks!

Theo

sandeep sankhlasandeep sankhla
Hi Theodosia,

You need to create a test class adn in that class you will be creating dummy records which will satsify few condition which you used in your trigger and class ..example

Insert a Opportunity Line iteam and then update the same in your test calss...

Provide the necessary data which you are using in your class to cover teh code..

Please try and let me know if you need any help..

Thanks,
Sandeep