+ Start a Discussion

Test Coverage - how to handle when walking through territory objects?

Hello.  I have a Trigger which queries the Territories associated with an Account.  To do this, I have to walk through the AccountShare and Group objects.  For each of these objects, I query an Id and put it into a Map.  How do I write test code for this?  So for example, I have the code shown below.  When I run tests, SFDC tells me that both of the lines inside the for loop should be covered.  But how?  I really don't know anything about them - I just store them and use them in the next query.


        for (AccountShare ash : [SELECT Id, UserOrGroupId FROM AccountShare
                    WHERE (RowCause = 'Territory' OR RowCause = 'TerritoryManual')
                    AND AccountId = :acctId ])
            String tmpid = ash.UserOrGroupId;
            asMap.put(tmpid , ash);


If the test results show that those two lines aren't covered, it means those lines of code aren't being executed in any test.  In a test method, you need to create sample data that will cause this query to actually return results so that the interior of the loop is executed.