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
murdocmurdoc 

Enhancing apex code coverage

I am trying to increase my code coverage from 59% to the required 75 for deployment. Below is my class.
 
public class ForecastedRevCon {
    public Integer size{get;set;}
    public Id oppId {get;set;}
    public Opportunity oppRec {get;set;}
    public String oppName {get; set;}
    
    public Decimal totExSelling {get;set;}
    public Decimal totNonExSelling {get;set;}
    public Decimal totSellingCredit {get;set;}
    public Decimal totExRevenue {get;set;}
    public Decimal totNonExRevenue {get;set;}
    public Decimal totOppRevenue {get;set;}
    
    public Decimal forRevTot {get;set;}
    
    public List<ForecastedRevConWrapper> listSellingCreditDet {get; set;}
    public List<ForecastedRevConWrapper> listRevenueCenter {get; set;}
    
    public List<ForecastedRevConWrapper> listSellingCreditDetDelete {get; set;}
    public List<ForecastedRevConWrapper> listRevenueCenterDelete {get; set;}
    
    public Map<String, Decimal> mapForcatedRevDet {get;set;}

    public ForecastedRevCon(ApexPages.StandardController stdCon) {
        
        oppId = Apexpages.currentPage().getParameters().get('oppId');
        init();
    }
    
    public void init(){
        Integer i = 0;
        
        forRevTot = 0;
        initRevDetMap();
        
        totExSelling = 0;
        totNonExSelling = 0;
        totSellingCredit = 0;
        totExRevenue = 0;
        totNonExRevenue = 0;
        totOppRevenue = 0;
        
        listSellingCreditDet = new List<ForecastedRevConWrapper>();
        listSellingCreditDetDelete = new List<ForecastedRevConWrapper>();
        
        listRevenueCenter = new List<ForecastedRevConWrapper>();
        listRevenueCenterDelete = new List<ForecastedRevConWrapper>();
        
        if(oppId !=null) {
            
            for(Opportunity op: [SELECT Id, Name,Event_Number__c  FROM Opportunity WHERE Id =: oppId]){
                oppRec = op;
                oppName = op.Name;
            }
            
            for(FreemanXP_Forecasted_Revenue_Detail__c FRD: [SELECT Id, 
                                                                   Sold_By__c,
                                                                   Amount__c, 
                                                                   Exclusive_Non__c,
                                                                   Non_Exclusive__c,
                                                                   Exclusive__c,
                                                                   Forecasted_Revenue_Type__c
                                                                   FROM FreemanXP_Forecasted_Revenue_Detail__c 
                                                                   WHERE Opportunity__c =: oppId order by createdDate]) {
                                                                        
                                                                        listSellingCreditDet.add(new ForecastedRevConWrapper(FRD, i));
                                                                        i++;
           }
           
           i = 0;
           for(FreemanXP_Revenue_Center_Details__c FRC: [SELECT Id,
                                                                Revenue_Center__c,
                                                                Opportunity__c,
                                                                Job_Number__c,
                                                                Exclusive__c,
                                                                Non_Exclusive__c,
                                                                Expo_Producing_Branch__c
                                                                FROM FreemanXP_Revenue_Center_Details__c
                                                                WHERE Opportunity__c =: oppId order by createdDate]){
                                                                    
                                                                    listRevenueCenter.add(new ForecastedRevConWrapper(FRC, i));
                                                                    i++;
           }
           calcTotal();
        }
        else{
            //handle if opportunity Id not supplied in param 
        }
    }
    
    public void initRevDetMap(){
        
        mapForcatedRevDet = new Map<String, Decimal>();
        
        /*mapForcatedRevDet.put('AlfordRevenue',0);
        mapForcatedRevDet.put('Cleaning',0);
        mapForcatedRevDet.put('ShowManagement',0);
        mapForcatedRevDet.put('Decorating',0);
        mapForcatedRevDet.put('Freight',0);
        mapForcatedRevDet.put('ExhibitTransportation',0);
        mapForcatedRevDet.put('Utilities',0);
        mapForcatedRevDet.put('Electrical',0);
        mapForcatedRevDet.put('OverheadRigging',0);
        mapForcatedRevDet.put('Technology',0);
        mapForcatedRevDet.put('Other',0);
        mapForcatedRevDet.put('Customs',0);
        mapForcatedRevDet.put('Alford',0);
        mapForcatedRevDet.put('AudioVisual',0);
        mapForcatedRevDet.put('DigitalServices',0);
        mapForcatedRevDet.put('Encore',0);
        mapForcatedRevDet.put('Entertainment',0);
        mapForcatedRevDet.put('ExperienceDesign',0);
        mapForecatedRevDet.put('ExhibitSurveys',0);
        mapForcatedRevDet.put('ProductionManagement',0);
        mapForcatedRevDet.put('Sponsorship',0);
        mapForcatedRevDet.put('SpecialEvents',0);
        mapForcatedRevDet.put('StrategyandUK',0);*/
        
        mapForcatedRevDet.put('Alford',0);
        mapForcatedRevDet.put('AudioVisual',0);
        mapForcatedRevDet.put('Cleaning',0);
        mapForcatedRevDet.put('CustomConstructionRevenue',0);
        mapForcatedRevDet.put('Decorating',0);
        mapForcatedRevDet.put('Electrical',0);
        mapForcatedRevDet.put('Encore',0);
        mapForcatedRevDet.put('ExhibitTransportation',0);
        mapForcatedRevDet.put('Freight',0);
        mapForcatedRevDet.put('FXPDigitalServices',0);
        mapForcatedRevDet.put('FXPEntertainment',0);
        mapForcatedRevDet.put('FXPExhibitSurveys',0);
        mapForcatedRevDet.put('FXPExperienceDesign',0);
        mapForcatedRevDet.put('FXPProductionManagementFee',0);
        mapForcatedRevDet.put('FXPSpecialEvents',0);
        mapForcatedRevDet.put('FXPStrategy',0);
        mapForcatedRevDet.put('Other',0);
        mapForcatedRevDet.put('OverheadRigging',0);
        mapForcatedRevDet.put('SeattleManagementFees',0);
        mapForcatedRevDet.put('ShowManagement',0);
        mapForcatedRevDet.put('FXPSponsorshipRevenue',0);
        mapForcatedRevDet.put('Technology',0);
        mapForcatedRevDet.put('UK',0);
        mapForcatedRevDet.put('Utilities',0);
    }

    public PageReference addRowSellingCredit() {
        Integer newindex = 0;
        
        String recType = Apexpages.currentPage().getParameters().get('recType');
        
        Apexpages.currentPage().getParameters().put('oppId', oppId );
        
        if(recType.equals('selling')){
            for(ForecastedRevConWrapper fx : listSellingCreditDet){
            newindex = fx.index;
            }
            newindex++;
        
            if(listSellingCreditDet != null){
                listSellingCreditDet.add(new ForecastedRevConWrapper(new FreemanXP_Forecasted_Revenue_Detail__c(Opportunity__c =oppId), newindex));
            }
        }
        else {
            for(ForecastedRevConWrapper fx : listRevenueCenter){
                newindex = fx.index;
            }
            newindex++;
            
            if(listRevenueCenter != null){
                listRevenueCenter.add(new ForecastedRevConWrapper(new FreemanXP_Revenue_Center_Details__c(Opportunity__c =oppId), newindex));
            }
        }
        return null;
    }
    
    public Pagereference deleteRowSellingCredit() {
        Integer index = Integer.valueOf(Apexpages.currentPage().getParameters().get('index'));
        String recType = Apexpages.currentPage().getParameters().get('recType');
        Integer delIndex = 0;
        
        if(recType.equals('selling')) {
            
            if(listSellingCreditDet.size() == 0){
                return null;
            }
                    
            for(Integer i=0;i<listSellingCreditDet.size(); i++){
                
                ForecastedRevConWrapper fx = listSellingCreditDet.get(i);
                if(fx.index == index){
                    fx.isDelete = true;
                    delIndex = i;
                    listSellingCreditDetDelete.add(fx);  
                }
            }
            listSellingCreditDet.remove(delIndex);
        }
        else {
            
            if(listRevenueCenter.size() == 0){
                return null;
            }
                    
            for(Integer i=0;i<listRevenueCenter.size(); i++){
                
                ForecastedRevConWrapper fx = listRevenueCenter.get(i);
                if(fx.index == index){
                    fx.isDelete = true;
                    delIndex = i;
                    listRevenueCenterDelete.add(fx); 
                }
            }
            listRevenueCenter.remove(delIndex);
        }
        
        calcTotal();
        
        return null;
    }
    
    public PageReference saveForecastedRev(){
        
        /*if(totSellingCredit != totOppRevenue){
            Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Error, Label.Selling_Credit_Save_Validation);
            Apexpages.addmessage(msg);
            return null;
        }*/
        
        List<FreemanXP_Forecasted_Revenue_Detail__c> listRecordUpsert = new List<FreemanXP_Forecasted_Revenue_Detail__c>();
        List<FreemanXP_Forecasted_Revenue_Detail__c> listRecordDelete = new List<FreemanXP_Forecasted_Revenue_Detail__c>();
        
        List<FreemanXP_Revenue_Center_Details__c> listRevenueUpsert = new List<FreemanXP_Revenue_Center_Details__c>();
        List<FreemanXP_Revenue_Center_Details__c> listRevenueDelete = new List<FreemanXP_Revenue_Center_Details__c>();
        
        for(ForecastedRevConWrapper fx : listSellingCreditDet){
            
            if(/*fx.wrapRecord.amount__c !=null && */!fx.isDelete) {
                listRecordUpsert.add(fx.wrapRecord);
            }
        }
        
        for(ForecastedRevConWrapper fx : listSellingCreditDetDelete){
            
            if(fx.isDelete && fx.wrapRecord.Id != null) {
                listRecordDelete.add(fx.wrapRecord);
            }
        }
        
        for(ForecastedRevConWrapper fx : listRevenueCenter){
            
            if(fx.revCenter.Revenue_Center__c !=null && !fx.isDelete) {
                listRevenueUpsert.add(fx.revCenter);
            }
        }
        
        for(ForecastedRevConWrapper fx : listRevenueCenterDelete){
            
            if(fx.isDelete && fx.revCenter.Id != null) {
                listRevenueDelete.add(fx.revCenter);
            }
        }
        
        try{
            if(!listRecordUpsert.isEmpty()){
                upsert listRecordUpsert;
            }
            if(!listRecordDelete.isEmpty()){
                delete listRecordDelete;
            }
            if(!listRevenueUpsert.isEmpty()){
                  
                    //if(listRevenueUpsert.get(0).Job_Number__c != null && listRevenueUpsert.get(0).Job_Number__c.length() <= 6){
                        //oppRec.Event_Number__c = listRevenueUpsert.get(0).Job_Number__c;
                    //}
                  for(FreemanXP_Revenue_Center_Details__c rcd: listRevenueUpsert){
                    if(rcd.Expo_Producing_Branch__c){
                      oppRec.Event_Number__c = rcd.Job_Number__c;
                    }
                  }
              upsert listRevenueUpsert;
            }
            if(!listRevenueDelete.isEmpty()){
                delete listRevenueDelete;
            }
            
            update oppRec;
            
            init();
            
            Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'Record(s) Saved Successfully!');
            Apexpages.addmessage(msg);
            return new PageReference('/'+oppRec.Id);
        }
        catch(Exception ex){
            Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Error,ex.getMessage());
            Apexpages.addmessage(msg);
        }
        
        return null;
    }
    
    public PageReference calcTotal(){
        
        initRevDetMap();
        forRevTot = 0;
        totExSelling = 0;
        totNonExSelling = 0;
        totSellingCredit = 0;
        totExRevenue = 0;
        totNonExRevenue = 0;
        totOppRevenue = 0;
        
        for(ForecastedRevConWrapper fx : listSellingCreditDet){
            try {
                totNonExSelling += (fx.wrapRecord.Non_Exclusive__c !=null ? fx.wrapRecord.Non_Exclusive__c: 0);
                totExSelling += (fx.wrapRecord.Exclusive__c !=null ? fx.wrapRecord.Exclusive__c: 0);
                
                decimal val = (fx.wrapRecord.Non_Exclusive__c !=null ? fx.wrapRecord.Non_Exclusive__c: 0) + (fx.wrapRecord.Exclusive__c !=null ? fx.wrapRecord.Exclusive__c: 0);
                totSellingCredit += val;
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Cleaning')){
                    updateValue('Cleaning', val);
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Decorating')){
                    updateValue('Decorating', val);
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Audio Visual')){
                    updateValue('AudioVisual', val);
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('FXP – Experience Design')){
                    updateValue('FXPExperienceDesign', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Show Management')){
                    updateValue('ShowManagement', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Freight')){
                    updateValue('Freight', val);
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Exhibit Transportation')){
                    updateValue('ExhibitTransportation', val);
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Utilities')){
                    updateValue('Utilities', val);
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Electrical')){
                    updateValue('Electrical', val);
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Overhead Rigging')){
                    updateValue('OverheadRigging', val);
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Technology')){
                    updateValue('Technology', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Other')){
                    updateValue('Other', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Custom Construction Revenue')){
                    updateValue('CustomConstructionRevenue', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Alford')){
                    updateValue('Alford', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('FXP – Digital Services')){
                    updateValue('FXPDigitalServices', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Encore')){
                    updateValue('Encore', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('FXP – Entertainment')){
                    updateValue('FXPEntertainment', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('FXP – Exhibit Surveys')){
                    updateValue('FXPExhibitSurveys', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('FXP – Production Management Fee')){
                    updateValue('FXPProductionManagementFee', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('FXP – Sponsorship Revenue')){
                    updateValue('FXPSponsorshipRevenue', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('FXP – Special Events')){
                    updateValue('FXPSpecialEvents', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('FXP – Strategy')){
                    updateValue('FXPStrategy', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('UK')){
                    updateValue('UK', val); 
                }
                if(fx.wrapRecord.Forecasted_Revenue_Type__c.equals('Seattle Management Fees')){
                    updateValue('SeattleManagementFees', val); 
                }
            }
            catch(Exception ex){}
        }
        
         for(ForecastedRevConWrapper fx : listRevenueCenter){
            
            try {
                
                totNonExRevenue += (fx.revCenter.Non_Exclusive__c !=null ? fx.revCenter.Non_Exclusive__c: 0);
                totExRevenue += (fx.revCenter.Exclusive__c !=null ? fx.revCenter.Exclusive__c: 0);
                
                decimal val = (fx.revCenter.Non_Exclusive__c !=null ? fx.revCenter.Non_Exclusive__c: 0) + (fx.revCenter.Exclusive__c !=null ? fx.revCenter.Exclusive__c: 0);
                totOppRevenue += val;
                                
             }
            catch(Exception ex){}
        }
        
        for(Decimal val: mapForcatedRevDet.values()){
            forRevTot += val;
        }
        
        return null;
    }

    public void updateValue(String key, Decimal v){
        Decimal ov = mapForcatedRevDet.get(key);
         ov += v;
         mapForcatedRevDet.put(key, ov);
    }
    
    public PageReference updateMainJob(){
      Integer index = Integer.valueOf(Apexpages.currentPage().getParameters().get('index'));
      
      
      List<FreemanXP_Revenue_Center_Details__c> listRev = new List<FreemanXP_Revenue_Center_Details__c>();
      
      if(listRevenueCenter.size() == 0){
        return null;
      }
              
      for(Integer i=0;i<listRevenueCenter.size(); i++){
 
          ForecastedRevConWrapper fx = listRevenueCenter.get(i);
          FreemanXP_Revenue_Center_Details__c rc = fx.revCenter;
          if(fx.index == index){
            rc.Expo_Producing_Branch__c = true;
            oppRec.Event_Number__c = rc.Job_Number__c;
          }
          else{
            rc.Expo_Producing_Branch__c = false;
          }
          listRev.add(rc);
      }
      
      try{
        if(!listRev.isEmpty()){
          upsert listRev;
          //oppRec.Event_Number__c = rc.Job_Number__c;
          update oppRec;
          init();
        }
      }
      catch(Exception ex){
         Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Error,ex.getMessage());
         Apexpages.addmessage(msg);
      }
      return null;
    }
        public class ForecastedRevConWrapper {
            public FreemanXP_Forecasted_Revenue_Detail__c wrapRecord {get;set;}
            public FreemanXP_Revenue_Center_Details__c revCenter {get;set;}
            public integer index {get;set;}
            public Boolean isDelete {get;set;}
            
            public ForecastedRevConWrapper(FreemanXP_Forecasted_Revenue_Detail__c obj, Integer index){
              this.wrapRecord = obj;
              this.index = index;
              this.isDelete = false;
            }
            
            public ForecastedRevConWrapper(FreemanXP_Revenue_Center_Details__c obj, Integer index){
              this.revCenter = obj;
              this.index = index;
              this.isDelete = false;
            }
     
       }
}
And here is my test Class:
@isTest
private class ForecastedRevConTest {
    
    private static Opportunity opp;
    
    static testMethod void myUnitTest() {
        
        createTestData();
        
        Test.startTest();
           ApexPages.currentPage().getParameters().put('id',opp.Id);
           
           ApexPages.StandardController std = new ApexPages.Standardcontroller(opp);
           
           ForecastedRevCon controller = new ForecastedRevCon(std);
           
           ApexPages.currentPage().getParameters().put('recType','selling');
           controller.addRowSellingCredit();
           controller.addRowSellingCredit();
           controller.addRowSellingCredit();
           ApexPages.currentPage().getParameters().put('recType','rev');
           controller.addRowSellingCredit();
           controller.addRowSellingCredit();
           controller.addRowSellingCredit();
           
           ApexPages.currentPage().getParameters().put('index',1+'');
           ApexPages.currentPage().getParameters().put('recType','selling');
           
           controller.deleteRowSellingCredit();
           ApexPages.currentPage().getParameters().put('index',1+'');
           ApexPages.currentPage().getParameters().put('recType','rev');
           
           controller.deleteRowSellingCredit();
           
           controller.saveForecastedRev();
           
           
        Test.stopTest();
    }
    
    static testMethod void myUnitTestB() {
        
        createTestData();
        
        Test.startTest();
           ApexPages.currentPage().getParameters().put('id',opp.Id);
           
           ApexPages.StandardController std = new ApexPages.Standardcontroller(opp);
           
           ForecastedRevCon controller = new ForecastedRevCon(std);
           
           ApexPages.currentPage().getParameters().put('recType','selling');
           controller.addRowSellingCredit();
           controller.addRowSellingCredit();
           controller.addRowSellingCredit();
           ApexPages.currentPage().getParameters().put('recType','rev');
           controller.addRowSellingCredit();
           controller.addRowSellingCredit();
           controller.addRowSellingCredit();
           
           ApexPages.currentPage().getParameters().put('index',2+'');
           ApexPages.currentPage().getParameters().put('recType','selling');
           
           controller.deleteRowSellingCredit();
           ApexPages.currentPage().getParameters().put('index',2+'');
           ApexPages.currentPage().getParameters().put('recType','rev');
           
           controller.deleteRowSellingCredit();
           
           controller.saveForecastedRev();
           
           
        Test.stopTest();
    }
        
    public static void createTestData(){
        Id rtId;
        Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ;
        Schema.SObjectType s = sObjectMap.get('Opportunity') ; // getting Sobject Type
        Schema.DescribeSObjectResult resSchema = s.getDescribe() ;
        Map<String,Schema.RecordTypeInfo> recordTypeInfo = resSchema.getRecordTypeInfosByName(); //getting all Recordtype for the Sobject
        rtId = recordTypeInfo.get('Exposition Services').getRecordTypeId();//particular RecordId by  Name
        
        Account a = new Account(Name = 'Test Account', 
                        Type = 'test type', 
                        fax='123456789x23', 
                        BillingStreet = 'test Street', 
                        BillingCity = 'test City', 
                        BillingCountry = 'test Country', 
                        BillingState = 'GA', 
                        BillingPostalCode = '23456', 
                        Phone = '123456789x24', 
                        Customer_Number__c = '123456' );                            
        insert a;                                         
            
        opp = new Opportunity(Name = 'Test Opp',
                                        RecordTypeId = rtId,
                                        AccountId = a.Id,
                                        Type = 'test type',
                                        New_Existing__c = 'New',
                                        CloseDate = date.today(),
                                        StageName = 'Proposed',
                                        Show_Management_Revenue_Est__c = 15100,
                                        Producing_Branch__c = 'FDC',/* ANAHEIM (128)',*/
                                        /*Show_Opening_Date__c = Date.today().addDays(5),*/
                                        Probability = 25,
                                        Event_Number__c = '234567'
                                        );
        insert opp;
        
    }
}
Any help would be greatly appreciated! As always it's urgent. :) Thanks!

 
Alain CabonAlain Cabon
Hi Murdoc,

You can see exactly what lines are not covered by your tests for ForecastedRevCon in the Developper Console.

There is a button at the bottom left "Code Coverage". 

Just look at the lines with a red bakground and fix your class of test.

59% your are very close to the end.

Below 100% code coverage so there is no red line just blue background for all the lines.

User-added image

Alain
murdocmurdoc
Alain,
Thanks for your insight. I can definitely see where the code is falling short, but I'm looking for some suggestions to increase the coverage. Any advice would be great.