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) {

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


territoryN is type of List<Territory>. Pay attention, that not just Territory. List is a container class in Apex. In order to debug names of territiries located in territoryN variable, you need to iterate list. Try:

for(Territory item :territoryN){
    System.debug('Territory name:' + item.Name);

instead of

System.debug('NAME TERRITORY******' + territoryN.Name);
| improve this answer | |

Please check below code and also run your query in "query editor"

List<Territory> territoryN = new List<Territory>([select Id, Name from Territory where Id in: territoryIds]);

 for(Territory tr:territoryN){
system.debug('****territory name******'+tr.name); }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.