0

I'm a newly hired Salesforce admin and I've been struggling to deploy this trigger since this code is completely new to me. I've been trying to deploy a new version of a trigger with, at most, 10 lines of code added (compared to previous version) to ensure a record type that has just been added subtracts the amount from each respective budget. I keep getting an error regarding code coverage, I'm at 40% currently and I need 75%. Here's the trigger:

trigger Comercial_Gesture on Comercial_Gesture__c (after update, after delete) {
    
    Id idRecordType_Station = Schema.SObjectType.Budget__c.getRecordTypeInfosByName().get('Station').getRecordTypeId();
    Id idRecordType_StrategicBusiness = Schema.SObjectType.Budget__c.getRecordTypeInfosByName().get('Strategic Business').getRecordTypeId();
    
    Id idRecordType_ADMW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('ADM Waiver').getRecordTypeId(); // amount_forgiven__c
    Id idRecordType_ExcBaggW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Excess Baggage Waiver').getRecordTypeId(); // Excess_baggage_cost__c
    Id idRecordType_FareW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Fare Waivers').getRecordTypeId(); // Fare_Difference__c
    Id idRecordType_GroupDRW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Group Deposit Return Waiver').getRecordTypeId(); // Deposit_Refund__c
    Id idRecordType_OverbookingDRW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Overbooking Waiver').getRecordTypeId(); // Overbooking_cost__c
    Id idRecordType_RevenueIncidentW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Revenue Incident Waiver').getRecordTypeId(); // amount_forgiven__c
    Id idRecordType_MalpracticeIncidentW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Malpractice Incident Waiver').getRecordTypeId(); //amount_forgiven__c
    Id idRecordType_InventoryActivationW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Inventory Activation Waiver').getRecordTypeId(); //amount_forgiven__c
    Id idRecordType_NDC_System_Error_Waiver = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('NDC System Error Waiver').getRecordTypeID(); //amount_forgiven_c
    //Instant
    Id idRecordType_Instant_Prefer = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Instant Prefer').getRecordTypeId();
    Id idRecordType_CopaClubPasses = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('COPA Club Pass').getRecordTypeId();
    Id idRecordType_Corporate = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Corporate').getRecordTypeId();
    
    List<Comercial_Gesture__c> listComercialGesture_Station = new List<Comercial_Gesture__c>();
    Map<String, Decimal> mapStation_ComercialG = new Map<String, Decimal>();
    Map<String, Budget__c> mapStation_Budget = new Map<String, Budget__c>();
    
    List<Comercial_Gesture__c> listComercialGesture_Strategic = new List<Comercial_Gesture__c>();
    Decimal decExpendedBudget = 0;
    Budget__c budgetStrategicBusiness;
    
    
    if(trigger.isAfter) {
        if(trigger.isUpdate || trigger.isDelete){  System.debug('ENTRE AL TRIGGER  ');
                                                 
            listComercialGesture_Station = [SELECT Station__c, 
                                                   amount_forgiven__c, 
                                                   Excess_baggage_cost__c, 
                                                   Fare_Difference__c, 
                                                   Deposit_Refund__c, 
                                                   Overbooking_cost__c, 
                                                   RecordTypeId 
                                                   FROM Comercial_Gesture__c WHERE Actual_Month__c = true AND Waiver_Type__c = 'Waiver Station Budget' AND Status__c = 'Approved' LIMIT 50000];    
                                                 
            for(Comercial_Gesture__c comercialG: listComercialGesture_Station) {
                if( !mapStation_ComercialG.containsKey(comercialG.Station__c) ) {
                    mapStation_ComercialG.put( comercialG.Station__c, 0);
                }
                //
                if( comercialG.RecordTypeId == idRecordType_ADMW ) {    
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 ) );
                }
                else if( comercialG.RecordTypeId == idRecordType_ExcBaggW ) {
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.Excess_baggage_cost__c != null ? comercialG.Excess_baggage_cost__c : 0 ) );
                }
                else if( comercialG.RecordTypeId == idRecordType_FareW ) {
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.Fare_Difference__c != null ? comercialG.Fare_Difference__c : 0 ) );
                }
                else if( comercialG.RecordTypeId == idRecordType_GroupDRW ) {
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.Deposit_Refund__c != null ? comercialG.Deposit_Refund__c : 0 ) );
                }
                else if( comercialG.RecordTypeId == idRecordType_OverbookingDRW ) {
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.Overbooking_cost__c != null ? comercialG.Overbooking_cost__c : 0 ) );
                }
                else if( comercialG.RecordTypeId == idRecordType_RevenueIncidentW ) {
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 ) );
                }
                else if( comercialG.RecordTypeId == idRecordType_MalpracticeIncidentW ) {
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 ) );
                }
                else if( comercialG.RecordTypeId == idRecordType_InventoryActivationW ) {
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 ) );
                }
                else if( comercialG.RecordTypeId == idRecordType_NDC_System_Error_Waiver ) {
                    mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 ) );
                }
            }
                                                 
            for(Budget__c budget: [SELECT Budget_Name__c, Expended_Budget_Station__c FROM Budget__c WHERE RecordTypeId =: idRecordType_Station]) {
                
                if( !mapStation_Budget.containsKey(budget.Budget_Name__c) ) {
                    mapStation_Budget.put( budget.Budget_Name__c, budget);
                    if( mapStation_ComercialG.containsKey(budget.Budget_Name__c) ) {
                        mapStation_Budget.get(budget.Budget_Name__c).Expended_Budget_Station__c = mapStation_ComercialG.get(budget.Budget_Name__c);
                    } /* condición alterna */
                    else {
                        mapStation_Budget.get(budget.Budget_Name__c).Expended_Budget_Station__c = 0;
                    }
                }
            }
                                                 
            listComercialGesture_Strategic = [SELECT amount_forgiven__c, 
                                                     Excess_baggage_cost__c, 
                                                     Fare_Difference__c, 
                                                     Deposit_Refund__c, 
                                                     Overbooking_cost__c, 
                                                     RecordTypeId 
                                                     FROM Comercial_Gesture__c WHERE Actual_Month__c = true AND Waiver_Type__c = 'Waiver Strategic Business Budget (HDQ)' AND Status__c = 'Approved' LIMIT 50000];
                                                 
            for(Comercial_Gesture__c comercialG: listComercialGesture_Strategic) {
                                                     
                if( comercialG.RecordTypeId == idRecordType_ADMW ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 );
                }
                else if( comercialG.RecordTypeId == idRecordType_ExcBaggW ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.Excess_baggage_cost__c != null ? comercialG.Excess_baggage_cost__c : 0 );
                }
                else if( comercialG.RecordTypeId == idRecordType_FareW ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.Fare_Difference__c != null ? comercialG.Fare_Difference__c : 0 );
                }
                else if( comercialG.RecordTypeId == idRecordType_GroupDRW ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.Deposit_Refund__c != null ? comercialG.Deposit_Refund__c : 0 );
                }
                else if( comercialG.RecordTypeId == idRecordType_OverbookingDRW ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.Overbooking_cost__c != null ? comercialG.Overbooking_cost__c : 0 );
                }
                else if( comercialG.RecordTypeId == idRecordType_RevenueIncidentW ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 );
                }
                else if( comercialG.RecordTypeId == idRecordType_MalpracticeIncidentW ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 );
                }
                else if( comercialG.RecordTypeId == idRecordType_InventoryActivationW ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 );
                }
                else if( comercialG.RecordTypeId == idRecordType_NDC_System_Error_Waiver ) {
                    decExpendedBudget = decExpendedBudget + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 );
                }
            }
                                                 
            budgetStrategicBusiness = [SELECT Expended_Budget_Strategic__c FROM Budget__c WHERE Budget_Name__c = 'Strategic Business' AND RecordTypeId =: idRecordType_StrategicBusiness LIMIT 1];
                                                 
            budgetStrategicBusiness.Expended_Budget_Strategic__c = decExpendedBudget;
                                                 
        }
        
    }
    
    if(trigger.isAfter){
        if(trigger.isDelete){
            Map<id,Account> corporate_account = new Map<id,Account>([select id, IPABGold__c, IPABSilver__c, IPABPlatinum__c, CCPABPasses__c from Account
where recordtypeid = :idRecordType_Corporate]);
            for(Comercial_Gesture__c cm : Trigger.old){
                Flow.Interview flow;
                Map<String,Object> params = new map<String,Object>();
                if(cm.RecordTypeId == idRecordType_Instant_Prefer){
                    params.put('RecordTypeInstantPrefer',cm.RecordTypeId);
                    params.put('curAccountID',cm.Account__c);
                    if(cm.Level_Requested__c == 'Member - Silver'){
                            params.put('FechaIPABSilver',corporate_account.get(cm.Account__c).IPABSilver__c);
                            flow = new Flow.Interview.Commercial_Silver_Count_on_Account(params);
                            flow.start();
                    }else if(cm.Level_Requested__c=='Silver - Gold'|| cm.Level_Requested__c=='Member - Gold'){
                        params.put('FechaIPABGold',corporate_account.get(cm.Account__c).IPABGold__c);
                            flow = new Flow.Interview.Commercial_Gold_Count_on_Account(params);                            
                            flow.start();
                    }else if(cm.Level_Requested__c == 'Silver - Platinum' || cm.Level_Requested__c=='Gold - Platinum' || cm.Level_Requested__c=='Member - Platinum'){
                            params.put('FechaIPABPlatinum',corporate_account.get(cm.Account__c).IPABPlatinum__c);
                            flow = new Flow.Interview.Commercial_Platinum_Count_on_Account(params);
                            flow.start();
                        }
                }else if (cm.RecordTypeId == idRecordType_CopaClubPasses){
                    params.put('FechaCCPABPasses',corporate_account.get(cm.Account__c).CCPABPasses__c);
                    flow = new Flow.Interview.Commercial_Passes_Count_on_Account(params);                    
                    flow.start();
                }
            }  
            
        }
    }
    
    List<Budget__c> listBudgetToBeUpdate = new List<Budget__c>();
    for(String str: mapStation_Budget.keySet() ) {
        listBudgetToBeUpdate.add(mapStation_Budget.get(str));
    }
    
    if( budgetStrategicBusiness != null ) {
        listBudgetToBeUpdate.add(budgetStrategicBusiness);
    }
    
    if( listBudgetToBeUpdate.size() > 0 ) {
        update listBudgetToBeUpdate;
    }

}
@isTest
private class Test_Trigger_Comercial_Gesture {
    static Profile p;
    static User u;
    static Account acc,acc2;
    static id idRecordType_ADMW,idRecordType_ExcBaggW,idRecordType_FareW,idRecordType_GroupDRW,
        idRecordType_OverbookingDRW,idRecordType_RevenueIncidentW,idRecordType_Station,idRecordType_StrategicBusiness,
        idRecordType_Instant_Prefer,idRecordType_CopaClubPasses,idRecordType_Corporate, idRecordType_NDC_System_Error_Waiver;
    
    static void init(){
        idRecordType_ADMW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('ADM Waiver').getRecordTypeId();
        idRecordType_ExcBaggW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Excess Baggage Waiver').getRecordTypeId();
        idRecordType_FareW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Fare Waivers').getRecordTypeId();
        idRecordType_GroupDRW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Group Deposit Return Waiver').getRecordTypeId();
        idRecordType_OverbookingDRW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Overbooking Waiver').getRecordTypeId();
        idRecordType_RevenueIncidentW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Revenue Incident Waiver').getRecordTypeId();
        idRecordType_NDC_System_Error_Waiver = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('NDC System Error Waiver').getRecordTypeID(); 
        
        idRecordType_Station = Schema.SObjectType.Budget__c.getRecordTypeInfosByName().get('Station').getRecordTypeId();
        idRecordType_StrategicBusiness = Schema.SObjectType.Budget__c.getRecordTypeInfosByName().get('Strategic Business').getRecordTypeId();
        
        idRecordType_Instant_Prefer = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('Instant Prefer').getRecordTypeId();
        idRecordType_CopaClubPasses = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('COPA Club Pass').getRecordTypeId();
        idRecordType_Corporate = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Corporate').getRecordTypeId();
        
        p = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];

        u = new User();
        u.Alias = 'usrcopa';
        u.Email = '[email protected]';
        u.LastName = 'Test';
        u.UserName = '[email protected]';
        u.TimeZoneSidKey = 'America/Los_Angeles';
        u.LocaleSidKey = 'en_US';
        u.LanguageLocaleKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8';
        u.ProfileId = p.Id;
        u.Station__c = 'PTY';
        insert u;
        
        acc = new Account();
        acc.Name = 'Test Acc';
        acc.OwnerId = u.Id;
        insert acc;
        
        acc2 = new Account();
        acc2.Name = 'Test Acc2';
        acc2.OwnerId = u.id;
        acc2.IPABSilver__c = System.now();
        acc2.IPABGold__c = System.now();
        acc2.IPABPlatinum__c = System.now();
        acc2.CCPABPasses__c = System.now();
        acc2.RecordTypeId = idRecordType_Corporate;
        acc2.No_Passes_Copa_s_Club__c = 10;
        acc2.No_Instant_PreferMember_Silver__c = 5;
        acc2.No_Instant_PreferMember_Gold__c = 5;
        acc2.No_Instant_PreferMember_Platinum__c = 5;
        insert acc2;

        List<Budget__c> listBudget = new List<Budget__c>();

        Budget__c budgetStation = new Budget__c();
        budgetStation.Budget_Name__c = 'PTY';
        budgetStation.RecordTypeId = idRecordType_Station;
        budgetStation.Waiver_Station_Budget__c = 100;
        budgetStation.Expended_Budget_Station__c = 0;
        listBudget.add(budgetStation);

        Budget__c budgetStrategic = new Budget__c();
        budgetStrategic.Budget_Name__c = 'Strategic Business';
        budgetStrategic.RecordTypeId = idRecordType_StrategicBusiness;
        budgetStrategic.Waiver_Strategic_Business_Budget__c = 100;
        budgetStrategic.Expended_Budget_Strategic__c = 0;
        listBudget.add(budgetStrategic);

        insert listBudget;
    }
    
    @isTest static void test_method_one() {
        init();
        List<Comercial_Gesture__c> listComGes = new List<Comercial_Gesture__c>();

        Comercial_Gesture__c comGes_1 = new Comercial_Gesture__c();
        comGes_1.RecordTypeId = idRecordType_ADMW;
        comGes_1.Account__c = acc.Id;
        comGes_1.Approval_Date__c = System.today();
        comGes_1.Waiver_Type__c = 'Waiver Station Budget';

        comGes_1.amount_forgiven__c = 1;    
        listComGes.add(comGes_1);

        Comercial_Gesture__c comGes_2 = new Comercial_Gesture__c();
        comGes_2.RecordTypeId = idRecordType_ExcBaggW;
        comGes_2.Account__c = acc.Id;
        comGes_2.Approval_Date__c = System.today();
        comGes_2.Waiver_Type__c = 'Waiver Station Budget';

        comGes_2.Excess_baggage_cost__c = 1;    
        listComGes.add(comGes_2);

        Comercial_Gesture__c comGes_3 = new Comercial_Gesture__c();
        comGes_3.RecordTypeId = idRecordType_FareW;
        comGes_3.Account__c = acc.Id;
        comGes_3.Approval_Date__c = System.today();
        comGes_3.Waiver_Type__c = 'Waiver Station Budget';

        comGes_3.OriginalFare__c = 2;
        comGes_3.PaidFare__c = 1;
        //comGes_3.TicketingIssuer__c = 'Agency';   
        listComGes.add(comGes_3);                   

        Comercial_Gesture__c comGes_4 = new Comercial_Gesture__c();
        comGes_4.RecordTypeId = idRecordType_GroupDRW;
        comGes_4.Account__c = acc.Id;
        comGes_4.Approval_Date__c = System.today();
        comGes_4.Waiver_Type__c = 'Waiver Station Budget';

        comGes_4.Refund_Deposit__c = 1; 
        listComGes.add(comGes_4);

        Comercial_Gesture__c comGes_5 = new Comercial_Gesture__c();
        comGes_5.RecordTypeId = idRecordType_OverbookingDRW;
        comGes_5.Account__c = acc.Id;
        comGes_5.Approval_Date__c = System.today();
        comGes_5.Waiver_Type__c = 'Waiver Station Budget';

        comGes_5.Assigned_class__c = 'Y';
        comGes_5.Available_class__c  = 'B';
        comGes_5.NumberofPassengers__c = 1;
        //comGes_5.TicketingIssuer__c = 'Agency';
        listComGes.add(comGes_5);
        
        Comercial_Gesture__c comGes_11 = new Comercial_Gesture__c();
        comGes_11.RecordTypeId = idRecordType_NDC_System_Error_Waiver;
        comGes_11.Account__c = acc.Id;
        comGes_11.Approval_Date__c = System.today();
        comGes_11.Waiver_Type__c = 'Waiver Station Budget';

        comGes_11.amount_forgiven__c = 1;    
        listComGes.add(comGes_11);

        /////
        Comercial_Gesture__c comGes_6 = new Comercial_Gesture__c();
        comGes_6.RecordTypeId = idRecordType_ADMW;
        comGes_6.Account__c = acc.Id;
        comGes_6.Approval_Date__c = System.today();
        comGes_6.Waiver_Type__c = 'Waiver Station Budget';

        comGes_6.amount_forgiven__c = 1;
        listComGes.add(comGes_6);

        Comercial_Gesture__c comGes_7 = new Comercial_Gesture__c();
        comGes_7.RecordTypeId = idRecordType_ExcBaggW;
        comGes_7.Account__c = acc.Id;
        comGes_7.Approval_Date__c = System.today();
        comGes_7.Waiver_Type__c = 'Waiver Station Budget';

        comGes_7.Excess_baggage_cost__c = 1;    
        listComGes.add(comGes_7);

        Comercial_Gesture__c comGes_8 = new Comercial_Gesture__c();
        comGes_8.RecordTypeId = idRecordType_FareW;
        comGes_8.Account__c = acc.Id;
        comGes_8.Approval_Date__c = System.today();
        comGes_8.Waiver_Type__c = 'Waiver Station Budget';

        comGes_8.OriginalFare__c = 2;
        comGes_8.PaidFare__c = 1;
        comGes_8.TicketingIssuer__c = 'City Ticket Office (CTO)';   
        listComGes.add(comGes_8);

        Comercial_Gesture__c comGes_9 = new Comercial_Gesture__c();
        comGes_9.RecordTypeId = idRecordType_GroupDRW;
        comGes_9.Account__c = acc.Id;
        comGes_9.Approval_Date__c = System.today();
        comGes_9.Waiver_Type__c = 'Waiver Station Budget';

        comGes_9.Refund_Deposit__c = 1; 
        listComGes.add(comGes_9);

        Comercial_Gesture__c comGes_10 = new Comercial_Gesture__c();
        comGes_10.RecordTypeId = idRecordType_OverbookingDRW;
        comGes_10.Account__c = acc.Id;
        comGes_10.Approval_Date__c = System.today();
        comGes_10.Waiver_Type__c = 'Waiver Station Budget';

        comGes_10.Assigned_class__c = 'Y';
        comGes_10.Available_class__c  = 'B';
        comGes_10.NumberofPassengers__c = 1;
        comGes_10.TicketingIssuer__c = 'City Ticket Office (CTO)';
        listComGes.add(comGes_10);
        
        Comercial_Gesture__c comGes_12 = new Comercial_Gesture__c();
        comGes_12.RecordTypeId = idRecordType_NDC_System_Error_Waiver;
        comGes_12.Account__c = acc.Id;
        comGes_12.Approval_Date__c = System.today();
        comGes_12.Waiver_Type__c = 'Waiver Station Budget';

        comGes_12.amount_forgiven__c = 1;    
        listComGes.add(comGes_12);


        insert listComGes;

        List<Comercial_Gesture__c> listComGes_Update = new List<Comercial_Gesture__c>();

        comGes_1.Status__c = 'Approved';
        listComGes_Update.add(comGes_1);
        comGes_2.Status__c = 'Approved';
        listComGes_Update.add(comGes_2);
        comGes_3.Status__c = 'Approved';
        listComGes_Update.add(comGes_3);
        comGes_4.Status__c = 'Approved';
        listComGes_Update.add(comGes_4);
        comGes_5.Status__c = 'Approved';
        listComGes_Update.add(comGes_5);
        comGes_11.Status__c = 'Approved';
        listComGes_Update.add(comGes_11);

        /////
        comGes_6.Status__c = 'Approved';
        listComGes_Update.add(comGes_6);
        comGes_7.Status__c = 'Approved';
        listComGes_Update.add(comGes_7);
        comGes_8.Status__c = 'Approved';
        listComGes_Update.add(comGes_8);
        comGes_9.Status__c = 'Approved';
        listComGes_Update.add(comGes_9);
        comGes_10.Status__c = 'Approved';
        listComGes_Update.add(comGes_10);
        comGes_12.Status__c = 'Approved';
        listComGes_Update.add(comGes_12);

        update listComGes_Update;
    }
    
    @isTest static void test_method_two(){
        init();
        List<Comercial_Gesture__c> listComGes = new List<Comercial_Gesture__c>();

        Comercial_Gesture__c comGes_1 = new Comercial_Gesture__c();
        comGes_1.RecordTypeId = idRecordType_Instant_Prefer;
        comGes_1.Account__c = acc2.Id;
        comGes_1.Approval_Date__c = System.today();
        comGes_1.Level_Requested__c = 'Member - Silver';
        comGes_1.ConnectMilesNumber__c = '124578963';
        comGes_1.NameofthePassenger__c = 'Test';
        comGes_1.Agreed_Amount__c = 10000;
        comGes_1.Revenue_Last_Year__c = 10000;
        comGes_1.Status_Last_Year__c = 'Member';
        comGes_1.Revenue_Last_Year__c   = 10000;
        comGes_1.Justification__c = 'Test';
        
        listComGes.add(comGes_1);
        
        Comercial_Gesture__c comGes_2 = new Comercial_Gesture__c();
        comGes_2.RecordTypeId = idRecordType_Instant_Prefer;
        comGes_2.Account__c = acc2.Id;
        comGes_2.Approval_Date__c = System.today();
        comGes_2.Level_Requested__c = 'Silver - Gold';
        comGes_2.ConnectMilesNumber__c = '124578963';
        comGes_2.NameofthePassenger__c = 'Test';
        comGes_2.Agreed_Amount__c = 10000;
        comGes_2.Revenue_Last_Year__c = 10000;
        comGes_2.Status_Last_Year__c = 'Member';
        comGes_2.Revenue_Last_Year__c   = 10000;
        comGes_2.Justification__c = 'Test';
        
        listComGes.add(comGes_2);
        
        Comercial_Gesture__c comGes_3 = new Comercial_Gesture__c();
        comGes_3.RecordTypeId = idRecordType_Instant_Prefer;
        comGes_3.Account__c = acc2.Id;
        comGes_3.Approval_Date__c = System.today();
        comGes_3.Level_Requested__c = 'Member - Platinum';
        comGes_3.ConnectMilesNumber__c = '124578963';
        comGes_3.NameofthePassenger__c = 'Test';
        comGes_3.Agreed_Amount__c = 10000;
        comGes_3.Revenue_Last_Year__c = 10000;
        comGes_3.Status_Last_Year__c = 'Member';
        comGes_3.Revenue_Last_Year__c   = 10000;
        comGes_3.Justification__c = 'Test';
        
        listComGes.add(comGes_3);
        
        Comercial_Gesture__c comGes_4 = new Comercial_Gesture__c();
        comGes_4.RecordTypeId = idRecordType_CopaClubPasses;
        comGes_4.Account__c = acc2.Id;
        comGes_4.Number_of_CopaClub_Passes__c = 5;
        comGes_4.NameofthePassenger__c = 'Test';
        
        listComGes.add(comGes_4);
        
        insert listComGes;
        List<Comercial_Gesture__c> listComGes_Update = new List<Comercial_Gesture__c>();

        comGes_1.Status__c = 'Approved';
        listComGes_Update.add(comGes_1);
        comGes_2.Status__c = 'Approved';
        listComGes_Update.add(comGes_2);
        comGes_3.Status__c = 'Approved';
        listComGes_Update.add(comGes_3);
        comGes_4.Status__c = 'Approved';
        listComGes_Update.add(comGes_4);

        update listComGes_Update;

        delete listComGes_Update;
    }
3
  • 1
    Welcome to SFSE!. Please see How to Ask on getting the most from the community. Please use edit to identify the missing coverage. You have so many unnecessary if conditions which makes test coverage more challenging. By clever use of maps and the Sobject methods get() and put() you can simplify this code way way down
    – cropredy
    Nov 29, 2023 at 22:01
  • There was a ton of extra whitespace in your trigger. Removing that allowed me to get your test class into the question itself.
    – Derek F
    Nov 29, 2023 at 23:40
  • 4
    Does this answer your question? How do I increase my code coverage, or why can't I cover these lines?
    – identigral
    Nov 30, 2023 at 0:01

1 Answer 1

2

Some background

The golden rule of unit testing is

You only gain coverage for lines of code that are executed by some test method

In tests, we are the ones responsible for setting up the test data. If you're not getting coverage for some lines of code, then one of the following must be true:

  • You haven't created the test data required to get you into the block(s) that aren't covered
  • Your test(s) are running into an exception that halts execution before it gets to the rest of the code in a given block

The issues I can see

It's hard to guess at exactly which lines of code here aren't covered, but a good place for me to start is with your third loop.

The data for that loop comes from a query, and the WHERE clause for that query is

FROM Comercial_Gesture__c WHERE Actual_Month__c = true AND Waiver_Type__c = 'Waiver Strategic Business Budget (HDQ)' AND Status__c = 'Approved' LIMIT 50000];

The LIMIT 50000 is concerning (it's a sign that this trigger needs to be restructured to work on a much smaller set of data), but the main issue here is that you never create any test Comercial_Gesture__c records where the Waiver Type is 'Waiver Strategic Business Budget (HDQ)'

If you don't have any records meeting all of the criteria in the WHERE clause, the query will return 0 rows. Salesforce will not execute the code inside of a loop if there are no records to loop over.

It's also not clear how Actual_Month__c is being set.

Not getting any records back from either your first or third queries wouldn't explain the 40% coverage figure you're claiming, but it is definitely something to look at.

Another issue is that you're never creating any test records for the idRecordType_RevenueIncidentW, idRecordType_MalpracticeIncidentW, or idRecordType_InventoryActivationW record types. Even if you are getting into your loops, you're not going to get into some of the else-ifs that are specific to those record types.

Recommendations

Test a single thing at a time

Yes, you can pile everything into a single test method, but that can end up making it hard to comply with the governor limits.

It also makes it harder to debug things when something goes wrong. If you end up encountering an error or if your calculations are off, how are you going to track down which record type is causing the issue?

If you break things into individual test methods (so that you're only creating 2 Comercial_Gesture__c records in each, one where Waiver_Type__c = Waiver Station Budget, and another where Waiver_Type__c = Waiver Strategic Business Budget (HDQ)), you (or the next person who has to work with this code) will have an easier time figuring out what specific parts aren't working if there are issues.

You need to add assertions

Code coverage is the only thing that Salesforce can realistically enforce, but the real value in unit tests are the assertions. The primary goal of testing, after all, is to detect issues more quickly.

You could get coverage for (and deploy) a class that has the following method

public static integer add2(Integer given) {
    return given + 3;
}

It's tests where you make an assertion that you would catch/realize the mistake when MyClass.add2(2) returned 5 instead of 4

The things that you assert are generally the "outputs", the results of running a particular method. In your case, one of the most obvious things to assert would be that the appropriate Budget__c record has Expended_Budget_Strategic__c set to the same value as the amount_forgiven__c for the appropriate Comercial_Gesture__c record in that particular test.

Reduce the amount of duplicated code

A rule of thumb in programming is

The more you type, the higher your chance of making a mistake

This isn't super related to testing per se, but it could help increase coverage.

There are 3 things that I see a lot of repetition for:

  • Id idRecordType_ADMW = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName().get('ADM Waiver').getRecordTypeId();
    
  • if( comercialG.RecordTypeId == idRecordType_ADMW ) {    
        mapStation_ComercialG.put( comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 ) );
    }
    
  • if( comercialG.RecordTypeId == idRecordType_ADMW ) {
        decExpendedBudget = decExpendedBudget + ( comercialG.amount_forgiven__c != null ? comercialG.amount_forgiven__c : 0 );
    }
    

If you call getRecordTypeInfosByName once, and store that in a Map<String, Schema.RecordTypeInfo>, you can save a good deal of typing on the rest of the lines where you store individual record type ids

Map<String, Schema.RecordTypeInfo> comercialRecTypesByName = Schema.SObjectType.Comercial_Gesture__c.getRecordTypeInfosByName();

Id idRecordType_ADMW = comercialRecTypesByName.get('ADM Waiver').getRecordTypeId();

For the other two, the only thing that's different in each if/else-if is what field you use. The field to use is completely determined by the record type. You can completely eliminate the large if/else-if chains by making use of a map (given record type x, use field y).

// Use curly braces instead of parenthesis when you want to initialize a
//   collection with >= 1 starting data
Map<Id, String> targetFieldByRecordTypeId = new Map<Id, String> {
    idRecordType_ADMW => 'amount_forgiven__c',
    idRecordType_ExcBaggW => 'idRecordType_ExcBaggW',
    idRecordType_FareW => 'Fare_Difference__c',
    idRecordType_GroupDRW => 'Deposit_Refund__c',
    idRecordType_OverbookingDRW => 'Overbooking_cost__c',
    idRecordType_RevenueIncidentW => 'amount_forgiven__c',
    idRecordType_MalpracticeIncidentW => 'amount_forgiven__c',
    idRecordType_InventoryActivationW => 'amount_forgiven__c',
    idRecordType_NDC_System_Error_Waiver => 'amount_forgiven__c'
};

...

for(Comercial_Gesture__c comercialG: listComercialGesture_Station) {
    if(!mapStation_ComercialG.containsKey(comercialG.Station__c) ) {
        mapStation_ComercialG.put(comercialG.Station__c, 0);
    }

    // This could all be one single line, but I've broken it out onto multiple
    //   lines for readability.
    String targetField = targetFieldByRecordTypeId.get(comercialG.RecordTypeId);

    // SObject's .get() allows us to use a string to get the value of a field
    // It does return an Object, so we need to do some typecasting
    Decimal targetValue = (Decimal)comercialG.get(targetField);
    targetValue = targetValue == null ? 0 : targetValue;

    // Using a primitive type (string, integer, decimal, etc...) as the value
    //   of a map means it gets stored by value rather than by reference.
    // That's why we need to do the dance of .get() to fetch the current value
    //   in order to update the value in the map
    mapStation_ComercialG.put(comercialG.Station__c, mapStation_ComercialG.get(comercialG.Station__c) + targetValue);

    // If you had a wrapper class that contains a decimal value, and used that
    //   as the value type for the map instead of Decimal, you could save yourself a
    //   little trouble.
    // You'd be able to directly act on/update the data inside the map instead of
    //   needing to do mymap.put(key, mymap.get(key) + value)
    // e.g.
    //mapStation_ComercialG.get(key).myDecimal += targetValue
}

// And you'd do something similar for the decExpendedBudget loop
1
  • +1 --my sentiments exactly to use the map and simplify the logic with sobject get() and put()
    – cropredy
    Nov 30, 2023 at 1:37

Not the answer you're looking for? Browse other questions tagged .