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
SF_MonkeySF_Monkey 

trigger test coverage

I have trying to get atleast the passing coverage for the trigger. Trigger is working fine but I am being able to get the test coverage. It would capture the oldMap and newMap values in the test
Trigger:
trigger allAccTrigger on Account (before update) {

    if(trigger.isUpdate){
        
        if(trigger.isBefore){
           if(checkRecursive.runOnce()){

            List<Account> accVert = new List<Account>();
            List<vendor_invoice__c> ven = new List<Vendor_Invoice__c>();

            for( Id accountId : Trigger.newMap.keySet() ){
          
            //Update the coordination checkbox after approval
            if(trigger.oldMap.get(accountId).Coordination_Complete__c != trigger.newMap.get(accountId).Coordination_Complete__c ){
               List<Processing_Checklist__c> prop = [select name from Processing_Checklist__c where account__c = :accountId AND name Like 'Coordination List Completed']; 
                prop[0].Completed__c = true;
                update prop;
            }
            
            
            if(trigger.oldMap.get(accountId).RecordTypeId != trigger.newMap.get(accountId).RecordTypeId){

                List<Account> accRecType = [select RecordTypeId from account where id = :accountId];

                //Check if all the vendor invoices have auditors before converting to contingency
                      if(trigger.newMap.get(accountId).RecordtypeId == '0120h000000MclWAAS'){
                            for(Vendor_Invoice__c v : [select name, id from vendor_invoice__c where account__c = :accountId AND Auditor_Assignment__c = null]){
                        ven.add(v);
                         }

                    if(ven.size()> 0){
                        Trigger.new[0].addError('Please assign auditor to ALL related vendor invoices');
                    }
                }


            //Checking to see if account moved to ACAP record type and deleting everything is vertical
                for(Account ac : accRecType){
 
                if(ac.RecordTypeId == '0120h000000YQmIAAW'){
                    accVert.add(ac);                        
                    }
                }
            }
        }
           
         //Getting all the verticals and navigation items to delete after the record is moved to ACAP   
                If(accVert.size() > 0)
                    {List<Prospect__c> props = [select id from Prospect__c where account__c IN :accVert];
                    List<Navigate_List__c> navList = [select id from Navigate_List__c where account__c IN :accVert];

                        delete props;
                        delete navList;
                    }
   
            }

       
        }
        
    }
}

Test
@isTest
public class accTriggerTest {
public static testMethod void casTrig(){
        Account accTest = new Account();
        accTest.Name = 'Test Account';
        accTest.Federal_Tax_Exempted__c = true;
        accTest.State_Tax_Exempted__c = false;
    	accTest.Coordination_Complete__c = false;
    	accTest.Company_Code__c = 'ads';
    	accTest.RecordTypeId = '0120h000000YQWeAAO';
        insert accTest;
        
        Opportunity opp = new opportunity();
        opp.AccountId = accTest.id;
        opp.Name = 'trying';
        opp.StageName = 'ACAP';
        opp.Estimated_Bill_Volume__c = 100;
        opp.CloseDate = Date.newInstance(2020, 12, 9);
        opp.Business_Development_Manager__c = UserInfo.getUserId();   
        opp.Business_Development_Rep__c = UserInfo.getUserId();
        opp.RecordTypeId = '0120h000000YQVlAAO';
        opp.Credit_Shared__c = 10;
        opp.Saving_Shared__c = 20;
        insert opp;
    
      	Vendor_Information__c venInfo = new Vendor_Information__c();
        venInfo.name = 'tester';
        venInfo.Abbreviated_Name__c = 'abc';
        venInfo.Phone__c = '123456789';
        insert venInfo;
    
        Vendor_Invoice__c venIn = new Vendor_Invoice__c();
    	venIn.Account_Number__c = 'fw df42r qe';
    	venIn.Account__c = accTest.Id;
    	venIn.Vendor_Information__c = venInfo.id;
        venIn.Federal_Tax_Exempted__c = true;
        venIn.State_Tax_Exempted__c = false;
    	venIn.Auditor_Assignment__c = UserInfo.getUserId();
        insert venIn;
        
        Account_Survey__c sur = new Account_Survey__c();
        sur.Account__c = accTest.id;
        insert sur;
        
        Case_Finding__c cas = new Case_Finding__c();
        cas.Name = 'try it';
        cas.Service_Type__c = 'Data';
        cas.Case_Date__c = Date.newInstance(2020, 12, 9);
        cas.Months_To_Bill__c = 12;
        cas.Vendor_Invoice__c = venIn.id;
    	cas.RecordTypeId = '0120h000000YQPxAAO';
    	cas.Case_Status__c = 'Active';
        insert cas;  

        Case_Finding__c cas1 = new Case_Finding__c();
        cas1.Name = 'try it';
        cas1.Service_Type__c = 'Data';
        cas1.Case_Date__c = Date.newInstance(2020, 12, 9);
        cas1.Months_To_Bill__c = 12;
        cas1.Vendor_Invoice__c = venIn.id;
    	cas1.RecordTypeId = '0120h000000i9oWAAQ';
    	cas1.Case_Status__c = 'Active';
        insert cas1; 
    	
        List<Case_Finding__c> cList = new List<Case_Finding__c>();
        cList.add(cas);
        cList.add(cas1);
    
        Processing_Checklist__c pro1 = new Processing_Checklist__c();
        pro1.Account__c = accTest.id;
        pro1.Name = 'Coordination List Completed';
        pro1.Notes__c = 'i really do not want to write the entire thing so i am going to end it here';
        pro1.Completed__c = false;
        insert pro1;
        
        Acap_Checklist__c pro = new Acap_Checklist__c();
        pro.Account__c = accTest.id;
        pro.Name = 'i dont know it yet';
        pro.Notes__c = 'i really do not want to write the entire thing so i am going to end it here';
        pro.Completed__c = false;
        insert pro;
        
    	accTest.RecordTypeId = '0120h000000YQmIAAW';
    	accTest.Coordination_Complete__c = true;
        update accTest;
            
   
    }
    
    public static testMethod void casTrig1(){
        Account accTest = new Account();
        accTest.Name = 'Test Account';
        accTest.Federal_Tax_Exempted__c = true;
        accTest.State_Tax_Exempted__c = false;
    	accTest.Coordination_Complete__c = false;
        accTest.Company_Code__c = 'er';
        insert accTest;
        
        opportunity opp = new opportunity();
        opp.AccountId = accTest.id;
        opp.Name = 'trying';
        opp.StageName = 'ACAP';
        opp.Estimated_Bill_Volume__c = 100;
        opp.CloseDate = Date.newInstance(2020, 12, 9);
        opp.Business_Development_Manager__c = UserInfo.getUserId();   
        opp.Business_Development_Rep__c = UserInfo.getUserId();
        opp.RecordTypeId = '0120h000000YQVlAAO';
        opp.Credit_Shared__c = 10;
        opp.Saving_Shared__c = 20;
        insert opp;
    
        Vendor_Information__c venInfo = new Vendor_Information__c();
        venInfo.name = 'tester';
        venInfo.Abbreviated_Name__c = 'abc';
        venInfo.Phone__c = '123456789';
        insert venInfo;
        
        Vendor_Invoice__c venIn = new Vendor_Invoice__c();
    	venIn.Account_Number__c = 'fw df42r qe';
    	venIn.Account__c = accTest.Id;
        venIn.Vendor_Information__c = venInfo.id;
        venIn.Federal_Tax_Exempted__c = true;
        venIn.State_Tax_Exempted__c = false;
        insert venIn;
        
        Account_Survey__c sur = new Account_Survey__c();
        sur.Account__c = accTest.id;
        insert sur;
        
        Case_Finding__c cas = new Case_Finding__c();
        cas.Name = 'try it';
        cas.Service_Type__c = 'LOCAL';
        cas.Case_Date__c = Date.newInstance(2020, 12, 9);
        cas.Months_To_Bill__c = 12;
        cas.Vendor_Invoice__c = venIn.id;
        cas.RecordTypeId = '0120h000000YQPxAAO';
        cas.Case_Status__c = 'Active';
        insert cas;  
    Test.startTest();
        try{
      accTest.RecordTypeId = '0120h000000MclWAAS';
            update accTest;
        system.assert(true, 'Please assign auditor to ALL related vendor invoices');}
        
        catch(exception ex){
            Boolean expectedExceptionThrown =  ex.getMessage().contains('Script-thrown exception') ? true : false;
            System.assertEquals(expectedExceptionThrown, false);
        }
        
        Test.stopTest();
    }
}

Coverage1st one2nd Coverage​​​​​​​
​​​​​​​Any help would be appreciated!
Ajay K DubediAjay K Dubedi
Hi,

I have gone through your Controller and test class and as i can see the first 'if condition' in your controller class
if(trigger.oldMap.get(accountId).Coordination_Complete__c != trigger.newMap.get(accountId).Coordination_Complete__c )
is not true. 
*Please type this code in your secount Test Method 'casTrig1()'
    accTest.Coordination_Complete__c = true;
    along with   accTest.RecordTypeId = '0120h000000MclWAAS';
                 update accTest;

And in your second method 'casTrig1()'
Please assign RecordTypeId to your Account object 
beacause your test class has gone through if(trigger.oldMap.get(accountId).RecordTypeId != trigger.newMap.get(accountId).RecordTypeId)
and since your account object doesn't have any value previously i.e., it does not contain any value in old map

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
SF_MonkeySF_Monkey
Thanks for the reply! It did not change anything on the coverage. 

Here is my updated test code
@isTest
public class accTriggerTest {
public static testMethod void casTrig(){
        Account accTest = new Account();
        accTest.Name = 'Test Account';
        accTest.Federal_Tax_Exempted__c = true;
        accTest.State_Tax_Exempted__c = false;
    	accTest.Coordination_Complete__c = false;
    	accTest.Company_Code__c = 'ads';
    	accTest.RecordTypeId = '0120h000000YQWeAAO';
        insert accTest;
        
        Opportunity opp = new opportunity();
        opp.AccountId = accTest.id;
        opp.Name = 'trying';
        opp.StageName = 'ACAP';
        opp.Estimated_Bill_Volume__c = 100;
        opp.CloseDate = Date.newInstance(2020, 12, 9);
        opp.Business_Development_Manager__c = UserInfo.getUserId();   
        opp.Business_Development_Rep__c = UserInfo.getUserId();
        opp.RecordTypeId = '0120h000000YQVlAAO';
        opp.Credit_Shared__c = 10;
        opp.Saving_Shared__c = 20;
        insert opp;
    
      	Vendor_Information__c venInfo = new Vendor_Information__c();
        venInfo.name = 'tester';
        venInfo.Abbreviated_Name__c = 'abc';
        venInfo.Phone__c = '123456789';
        insert venInfo;
    
        Vendor_Invoice__c venIn = new Vendor_Invoice__c();
    	venIn.Account_Number__c = 'fw df42r qe';
    	venIn.Account__c = accTest.Id;
    	venIn.Vendor_Information__c = venInfo.id;
        venIn.Federal_Tax_Exempted__c = true;
        venIn.State_Tax_Exempted__c = false;
    	venIn.Auditor_Assignment__c = UserInfo.getUserId();
        insert venIn;
        
        Account_Survey__c sur = new Account_Survey__c();
        sur.Account__c = accTest.id;
        insert sur;
        
        Case_Finding__c cas = new Case_Finding__c();
        cas.Name = 'try it';
        cas.Service_Type__c = 'Data';
        cas.Case_Date__c = Date.newInstance(2020, 12, 9);
        cas.Months_To_Bill__c = 12;
        cas.Vendor_Invoice__c = venIn.id;
    	cas.RecordTypeId = '0120h000000YQPxAAO';
    	cas.Case_Status__c = 'Active';
        insert cas;  

        Case_Finding__c cas1 = new Case_Finding__c();
        cas1.Name = 'try it';
        cas1.Service_Type__c = 'Data';
        cas1.Case_Date__c = Date.newInstance(2020, 12, 9);
        cas1.Months_To_Bill__c = 12;
        cas1.Vendor_Invoice__c = venIn.id;
    	cas1.RecordTypeId = '0120h000000i9oWAAQ';
    	cas1.Case_Status__c = 'Active';
        insert cas1; 
    	
        List<Case_Finding__c> cList = new List<Case_Finding__c>();
        cList.add(cas);
        cList.add(cas1);
    
        Processing_Checklist__c pro1 = new Processing_Checklist__c();
        pro1.Account__c = accTest.id;
        pro1.Name = 'Coordination List Completed';
        pro1.Notes__c = 'i really do not want to write the entire thing so i am going to end it here';
        pro1.Completed__c = false;
        insert pro1;
        
        Acap_Checklist__c pro = new Acap_Checklist__c();
        pro.Account__c = accTest.id;
        pro.Name = 'i dont know it yet';
        pro.Notes__c = 'i really do not want to write the entire thing so i am going to end it here';
        pro.Completed__c = false;
        insert pro;
        
    	accTest.RecordTypeId = '0120h000000YQmIAAW';
    	accTest.Coordination_Complete__c = true;
        update accTest;
            
   
    }
    
    public static testMethod void casTrig1(){
        Account accTest = new Account();
        accTest.Name = 'Test Account';
        accTest.Federal_Tax_Exempted__c = true;
        accTest.State_Tax_Exempted__c = false;
    	accTest.Coordination_Complete__c = false;
        accTest.Company_Code__c = 'er';
        accTest.RecordTypeId = '0120h000000YQWeAAO';
        insert accTest;
        
        opportunity opp = new opportunity();
        opp.AccountId = accTest.id;
        opp.Name = 'trying';
        opp.StageName = 'ACAP';
        opp.Estimated_Bill_Volume__c = 100;
        opp.CloseDate = Date.newInstance(2020, 12, 9);
        opp.Business_Development_Manager__c = UserInfo.getUserId();   
        opp.Business_Development_Rep__c = UserInfo.getUserId();
        opp.RecordTypeId = '0120h000000YQVlAAO';
        opp.Credit_Shared__c = 10;
        opp.Saving_Shared__c = 20;
        insert opp;
    
        Vendor_Information__c venInfo = new Vendor_Information__c();
        venInfo.name = 'tester';
        venInfo.Abbreviated_Name__c = 'abc';
        venInfo.Phone__c = '123456789';
        insert venInfo;
        
        Vendor_Invoice__c venIn = new Vendor_Invoice__c();
    	venIn.Account_Number__c = 'fw df42r qe';
    	venIn.Account__c = accTest.Id;
        venIn.Vendor_Information__c = venInfo.id;
        venIn.Federal_Tax_Exempted__c = true;
        venIn.State_Tax_Exempted__c = false;
        insert venIn;
        
        Account_Survey__c sur = new Account_Survey__c();
        sur.Account__c = accTest.id;
        insert sur;
        
        Case_Finding__c cas = new Case_Finding__c();
        cas.Name = 'try it';
        cas.Service_Type__c = 'LOCAL';
        cas.Case_Date__c = Date.newInstance(2020, 12, 9);
        cas.Months_To_Bill__c = 12;
        cas.Vendor_Invoice__c = venIn.id;
        cas.RecordTypeId = '0120h000000YQPxAAO';
        cas.Case_Status__c = 'Active';
        insert cas;  
    Test.startTest();
        try{
            accTest.Coordination_Complete__c = true;
            accTest.RecordTypeId = '0120h000000MclWAAS';
            update accTest;
            system.assert(true, 'Please assign auditor to ALL related vendor invoices');}
        
        catch(exception ex){
            Boolean expectedExceptionThrown =  ex.getMessage().contains('Script-thrown exception') ? true : false;
            System.assertEquals(expectedExceptionThrown, false);
        }
        
        Test.stopTest();
    }
}