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
Will BauzonWill 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:
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!
 
Best Answer chosen by Will Bauzon
Shashikant SharmaShashikant Sharma
What is its Formula, Just create Data so that formula has that value. In case you have difficulty in doing that then you just could do one change in your trigger code.

// add testing value in test class context
if( Test.isRunningTest() ) {
   setName.add( 'NAM|SMB|FL' ) ;
}


See above change in your trigger code.
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);
        }   
    }   
   
// add testing value in test class context 
if( Test.isRunningTest() ) {
   
    setName.add(  'NAM|SMB|FL' );

}

    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;
             
              }
           }
        }
        
    }
        
}



This would give you the code coverage.

Thanks
Shashikant

 

All Answers

Shashikant SharmaShashikant Sharma
Hi Will,

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.
 
@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;

       // set RoutingKey value on account
       a.RoutingKey__c = 'NAM|SMB|FL';

       insert a;
       
       a.RunAssignment__c  =  FALSE;
       a.Territory__c = t.id;
       update a;
       
    }
    
}

Please let me know if you still face issues.

Thanks
Shashikant
 
Will BauzonWill Bauzon
Hi Shashikant - a.RoutingKey__c is a formula field that is set using the Override fields. Since the field isn't writeable, I can't set the value in the test class.
Shashikant SharmaShashikant Sharma
What is its Formula, Just create Data so that formula has that value. In case you have difficulty in doing that then you just could do one change in your trigger code.

// add testing value in test class context
if( Test.isRunningTest() ) {
   setName.add( 'NAM|SMB|FL' ) ;
}


See above change in your trigger code.
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);
        }   
    }   
   
// add testing value in test class context 
if( Test.isRunningTest() ) {
   
    setName.add(  'NAM|SMB|FL' );

}

    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;
             
              }
           }
        }
        
    }
        
}



This would give you the code coverage.

Thanks
Shashikant

 
This was selected as the best answer
Shashikant SharmaShashikant Sharma
Hi Will,

Do you still have issues in code coverage ?

Thanks
​Shashikant
Will BauzonWill Bauzon
Hi Shashikant - the trigger was deployed successfully! Thank you!