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
Daniel Martins 4Daniel Martins 4 

Error in Territory Name

Hi Guys,
I need to get the Name of the Territory to assign it to a field, but when I try it, it generates an error.

this is my code:

 List<UserTerritory> myList = [SELECT Id, UserId, TerritoryId 
                                      FROM UserTerritory 
                                      WHERE UserId = :UserInfo.getUserId()];
        
        Set<Id> territoryIds = new Set<Id>();
        for(UserTerritory terr : myList) {
         territoryIds.add(terr.TerritoryId);
        }
        
        List<Territory> territoryN = new List<Territory>([SELECT Id, Name
                                                             FROM Territory
                                                             WHERE Id = :territoryIds LIMIT 1]);

        System.debug('NAME TERRITORY******' + territoryN.Name);

in the System.Debug I need view the Name (territoryN.Name)  and the error is "Variable does not exist: Name"

Please help me 
GauravGargGauravGarg
Hi Daniel,

You are accessing a list just a like a record, which isn't allowed or I should say SF will not identify which record Name need to display. 

So try like this
 
for(Territory t : territoryN )
{
    system.debug('Territory name: '+ t.Name);
}
Or,
if you like to query only one record. 
 
territoryN[0].Name


This will help. Let me know if you still face some error. 

Thanks,

Gaurav
Skype: gaurav62990

Daniel Martins 4Daniel Martins 4
Thank you so much GauravGarg, This work ritght now, regards
GauravGargGauravGarg
your welcome Daniel, if you have more complex requirement you can post me...I will help you out.
GauravGargGauravGarg
Hi Daniel,

Can you close this post. By marking it as solved. 

Thanks,
Gaurav
skype: gaurav62990