+ Start a Discussion
Saad Ahmad 27Saad Ahmad 27 

Covering For Loops in a test class

I have writted the following trigger along with the test below. I'm unable to figure out how to cover the for loop in my test class. Looking forward to your advise.
 
List<Account> accountUpdatedList = new List<Account>();
    Map<string,string> territoryWithParentTerritoryNameMap = new map<string,string>();
    for(Territory2 tr : [SELECT id, Name,Territory2.ParentTerritory2.Name FROM Territory2 WHERE Territory2.ParentTerritory2.Name != Null Limit 50000])
    {
        territoryWithParentTerritoryNameMap.put(tr.name,tr.ParentTerritory2.Name);
    }
        
    for (Account ac : Trigger.new) {
        ac.Sales_Rep_Region__c = ' ';
        Integer counter = 0;
        if (ac.Territories__c != null) {
            for (String t : ac.Territories__c.split(',')) {
                counter += 1;
            }
            ac.Team_count__c = counter - 3;
        }
    
    if(ac.Territories__c != Null && ac.Territory_Has_Changed__c != False) 
        {
            for(string str : ac.Territories__c.split(',')){
                if(str != '104B' && str != '109B' && str != '109A' && str != ac.RVP_Code__c && str != '104A'){ 
                    if(territoryWithParentTerritoryNameMap.containsKey(str)){
                    
                        string tempParentTerritoryName = territoryWithParentTerritoryNameMap.get(str);
                        if(ac.RVP_Code__c != tempParentTerritoryName){                                
                            if(!ac.Sales_Rep_Region__c.contains(str)){                                    
                                ac.Sales_Rep_Region__c += str + ',';
                                accountUpdatedList.add(ac);   
                                    
                            }
                            
                        }
                       
                    }
                    
                }
              
            }
        }
        
    }

Test:
@istest
private class AccountTerritoryCount_test {
    static testMethod void TestAccount(){
        Account acc = new Account(name='test', 
                              jde_account_number__c='1234',
                              Territories__c='1234,1234,1234,1234,1234,1224,1234,1234,1234,');
    	insert acc;
        
    }
    
    static testMethod void myAccountTest()
    {
        List<Account> accountUpdatedList = new List<Account>();
        Map<string,string> territoryWithParentTerritoryNameMap = new map<string,string>();
    	for(Territory2 tr : [SELECT id, Name,Territory2.ParentTerritory2.Name FROM Territory2 
                         WHERE Territory2.ParentTerritory2.Name != '109N' Limit 50000])
        {
        	territoryWithParentTerritoryNameMap.put(tr.name,tr.ParentTerritory2.Name);
        }
        
        Account ac = new Account();
        ac.name = 'Toronto';
        ac.JDE_Account_Number__c = '12345678';
        ac.Territories__c = '1011,1436';
        ac.RVP_Code__c = '109N';
        insert ac;
        
        Account ac2 = new Account();
        ac2.name = 'Toronto2';
        ac2.JDE_Account_Number__c = '12345679';
        ac2.Territories__c = '1011,1436';
        ac2.RVP_Code__c = '109N';
        insert ac2;
        
        ac = [SELECT Sales_Rep_Region__c,Territories__c from Account where Id =:ac.Id];
        
        system.assertEquals(ac.Sales_Rep_Region__c, null);
        
    }
    
}
Lines 
User-added image
Best Answer chosen by Saad Ahmad 27
Gururaj BGururaj B
It all depends on how you have configured the field "ac.Territory_Has_Changed__c". Assuming you are setting this field to true using process builder, After trigger will be executed before the Process builder is executed. So when a new record is created this field will remain either null or default value. So its not entering the if condition and so the for loop is not covered. I hope this helps, if so please mark as best answer.

All Answers

Gururaj BGururaj B
It all depends on how you have configured the field "ac.Territory_Has_Changed__c". Assuming you are setting this field to true using process builder, After trigger will be executed before the Process builder is executed. So when a new record is created this field will remain either null or default value. So its not entering the if condition and so the for loop is not covered. I hope this helps, if so please mark as best answer.
This was selected as the best answer
Saad Ahmad 27Saad Ahmad 27
Thanks Gururaj! That makes sense. I was able to resolve this issue thanks to your suggestion.