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 

Trigger test class code coverage

For my trigger I'm getting only 40% code coverage. I'm unbale to cover the following code, though I wrote some code here. Guys can you please help me here what should be the test class code for the following piece of code.

List<Commission__c> existingCommissions = [SELECT Id, Name__c, Sales__c, Status__c, Account__c, Description__c, Rule_ID__c FROM Commission_Meter__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_Meter__c cmNew = triggerCommissionMetersMap.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); } }
Lars NielsenLars Nielsen
You're code is jumbled so I have reformated it here. I think the reformatting is correct but take a look.
 
List<Commission__c> existingCommissions = [SELECT Id, Name__c, Sales__c, Status__c, Account__c, Description__c, Rule_ID__c FROM Commission_Meter__c where Rule_ID__c IN :ruleIDs and account__c IN :accountids and ID NOT IN :trigger.new limit 10000];

//start of for loop
 for(Commission__c cm : existingCommissions){
   string keypoint = cm.Rule_ID__c+cm.account__c;
   Commission_Meter__c cmNew = triggerCommissionMetersMap.get(keypoint); 
 
   //start outer if statement
   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); 
} //end of outer if statement
 
} //end of for loop

It looks like you are just building a List of Commission__c with a name of existingCommissions. Typically, in a code like this I would create a test class with setup code that inserts,updates or edits records so that you can verify that existingCommissions comes back populated first - everythng else seems to depend on that. i.e. you test make sure to test what happens when existingCommissions > 0 and when it existingCommissions = 0;

Since Rule_ID__c, Commission_Meter__c and Commission__c  are all custom it's hard to say what the goal of all that is. Can you provide anymore details?