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
Laxmaya ChnLaxmaya Chn 

Unable to cover code for a trigger.

Hi guys, I'm unable to cover code for my trigger. As I'm new in coding I'm facing issues. Here unable to cover the code in bold format and I'm getting only 37% code coverage. Can you please suggest/give tips/code your ideas?. Thanks in Advance.

TRIGGER::
trigger Commission_Trigger on Commission__c (after insert, after update) {
    try{
        if(Trigger.isAfter && Trigger.isInsert && !CommissionHelper.isCommissionTgrAlreadyRunning){
            
            CommissionHelper.isCommissionTgrAlreadyRunning = true;
            set<id> accountids = new set<ID>();
            List<string> ruleIDs = new List<string>();
            map<id,account> existingaccountmap = new map<id,account>();
            
            List<ID> toDeleteCommission = new List<ID>();
            Map<string,Commission__c> triggerCommissionsMap = new Map<string,Commission__c> ();
            List<Commission__c> finalUpdate = new List<Commission__c>();
            
            for(Commission__c cm : Trigger.new){
                if(string.isNotBlank(cm.Rule_ID__c)&& (accountids <> NULL) ){
                    triggerCommissionsMap.put(cm.Rule_ID__c+cm.account__c,cm); 
                    ruleIDs.add(cm.Rule_ID__c);
                    accountids.add(cm.account__c);               
                }
            }
List<Commission__c> existingCommissions = [SELECT Id, Name__c, Sales__c, Status__c, Account__c, Description__c, Rule_ID__c FROM Commission__c where Rule_ID__c IN :ruleIDs and account__c IN :accountids and ID NOT IN :trigger.new limit 10000];

            for(Commission__c cm : existingCommissions){
            string keypoint = cm.Rule_ID__c+cm.account__c;
                Commission__c cmNew = triggerCommissionsMap.get(keypoint);
                if(cmNew != null && string.isNotBlank(cmNew.Rule_ID__c) && string.isNotBlank(cmNew.Account__c) && cmNew.Rule_ID__c.equals(cm.Rule_ID__c) && cmNew.Account__c.equals(cm.Account__c) ){

                    if(string.isNotBlank(cmNew.Rule_ID__c))cm.Rule_ID__c = cmNew.Rule_ID__c;
                    if(string.isNotBlank(cmNew.Name__c))cm.Name__c = cmNew.Name__c;
                    if((cmNew.Sales__c != null))cm.Sales__c = cmNew.Sales__c;
                    if(string.isNotBlank(cmNew.Status__c))cm.Status__c = cmNew.Status__c;
                    if(string.isNotBlank(cmNew.Description__c))cm.Description__c = cmNew.Description__c;

            finalUpdate.add(cm);                    
            todeleteCommission.add(cmNew.id);
             
}
            }
            system.debug('The finalUpdate Info::'+finalUpdate);
            if(finalUpdate.size() > 0){
                system.Database.update(finalUpdate);
                CommissionHelper.toDeleteCommission(toDeleteCommission);    
            }
            
        }else{
            //Do nothing
            system.debug('This trigger logic is not executing 2nd time');
        }
    }catch(Exception ex){
        system.debug('Some thing went wrong::EX:'+ex);
    }
}
    
TEST CLASS:
@isTest
public class Commission_Trigger_Test {
    public static testMethod void testCommissiontrigger() {
               
        List<Account> act = new List<Account>{ new Account(
            Name = 'Jeffy',
            Brands = 'Appliances',
            Account_Manager__c = 'DougBolo') };
                  
        insert act;
                 
        List<Commission__c> comtr = new List<Commission_Meter__c>{ new Commission__c(
            Description__c = 'Books',
            Rule_ID__c = 'R5522',
            Sales__c = 122,
            Status__c = 145,
            Account__c = [SELECT id FROM Account LIMIT 1][0].id,
            Name__c = 'Active')};
                 
        insert comtr; 
        update comtr;
        
      List<Commission__c> updtcomtr = New List<Commission__c>{
            [SELECT Id, Name__c, Status__c, Account__c, Description__c, Sales__c, Description__c FROM Commission__c where id in :comtr]                for(Commission__c ucomtr:updtcomtr){               
                ucomtr.Name__c = 'zhan11';
               ucomtr.Status__c = 'Processing';
               ucomtr.Description__c = 'New Rule';
               ucomtr.Sales__c = 1000;                
               ucomtr.Rule_ID__c = 'Z443';                           
        }        
        update ucomtr;                
    }
}
Sumeet_ForceSumeet_Force
Hi,
Make sure to cover below scenarios:
1. The existingCommissions variable should get some records so that looping part is executed and covered.
2. The finalUpdate variable should have value greater than zero to be able to call the toDeleteCommission on CommissionHelper object.

Above 2 points shall definitely bring your coverage to a very high %.
Laxmaya ChnLaxmaya Chn
Hi Sumeet, I've already taken for loop to cover those records, need to take more records?.
Sumeet_ForceSumeet_Force

I gave it a try by modifying all fields to Name, Id of account object and it reached 56%. Since queries like ID IN :ruleIDs and ID IN :accountids didnt return any records in my case, it was limited to 56% else it would have completed 100%. I suggest you to ensure that test data is able to execute all lines...you can try using seealldata annotation in your test class if you want dont want to create test data yourself.

Also , you can comment different parts of code during different test executions to exact area which is not getting covered and why.
Sumeet_ForceSumeet_Force
Did above suggestion help ?