You need to sign in to do that
Don't have an account?
Will Bauzon
Help with code coverage on trigger
Hello there,
I'm still pretty new to code development and I am using the following trigger and class.
Trigger:
Test Class:
I'm stuck at 72% coverage and would like to increase as much as possible. I looked at my dev console and lines 17, 18, and 27-30 are uncovered by my class. I'm not certain how to write the class to cover these lines and the account update doesn't cover it. Can someone help/explain what I can do to fix this? Thank you in advance for the help!
I'm still pretty new to code development and I am using the following trigger and class.
Trigger:
trigger AccountTerritoryTag on Account (before insert, before update) { Set<String> setName = new Set<String>(); for (Account account : Trigger.new) { if (account.RunAssignment__c == TRUE) { setName.add(account.RoutingKey__c); } } if(setName.size() > 0 ) { List<RoutingKey__c> routingkey = [select Name,Territory__c from RoutingKey__c where Name in :setName ]; Map<String, RoutingKey__c> mapNameWiseR = new Map<String, RoutingKey__c> (); for(RoutingKey__c RK : routingkey ) { mapNameWiseR.put(RK.Name , RK); } for (Account account : Trigger.new) { if (account.RunAssignment__c == TRUE) { if (mapNameWiseR.containsKey(account.RoutingKey__c) ) { RoutingKey__c rk = mapNameWiseR.get(account.RoutingKey__c); //assign the territory account.Territory__c = rk.Territory__c; account.RunAssignment__c = FALSE; } } } } }
Test Class:
@isTest private class AccountTerritoryTest { static testMethod void AccountTerritoryTest() { Territory__c t = new Territory__c(); t.Name = 'TerritoryTest'; insert t; RoutingKey__c k = new RoutingKey__c(); k.Name = 'NAM|SMB|FL'; k.Territory__c = t.id; insert k; Account a = new Account(); a.Name = 'Test'; a.OverrideCountry__c = 'US'; a.OverrideEmployees__c = 'A) 1-100'; a.OverrideState__c = 'FL'; a.RunAssignment__c = TRUE; insert a; a.RunAssignment__c = FALSE; a.Territory__c = t.id; update a; } }
I'm stuck at 72% coverage and would like to increase as much as possible. I looked at my dev console and lines 17, 18, and 27-30 are uncovered by my class. I'm not certain how to write the class to cover these lines and the account update doesn't cover it. Can someone help/explain what I can do to fix this? Thank you in advance for the help!
// add testing value in test class context
if( Test.isRunningTest() ) {
setName.add( 'NAM|SMB|FL' ) ;
}
See above change in your trigger code.
This would give you the code coverage.
Thanks
Shashikant
All Answers
You need to add the RunAssignmentKey__c value on Account record when you are inserting. It would allow Query to find the record and then map contains condition will satisfy the condition at line 27.
Please see updated code.
Please let me know if you still face issues.
Thanks
Shashikant
// add testing value in test class context
if( Test.isRunningTest() ) {
setName.add( 'NAM|SMB|FL' ) ;
}
See above change in your trigger code.
This would give you the code coverage.
Thanks
Shashikant
Do you still have issues in code coverage ?
Thanks
Shashikant