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
Matyas CsabaMatyas Csaba 

Accounts in a given Territory

Hello Helper

I need to retrieve by  code all accouts  belongong to a given  territory (and subordinates but this is secondary)

I know the info  is  somewhere in AccountShare

My first attempt was to  retrieve all users  belonging  to the territory from UserTerritory object  and then run a query  against  the AccountShare 
where UserGroupId in (the user  resulted  from Userterritory  query)

BUT  this is not  enough. If  no  User  is assigned to the terr I can not find the Accounts

Other option?

I am thinking to  query  first  the Group Object  and find those groupID  where RelatedId is  my Territlry
then go  with these  UserGroupId back  to AccountShare

but this might not be enough  again because  of the group  hierarchy

Did anybody  worked  on this topic?

Thanks in advance
NagendraNagendra (Salesforce Developers) 
Hi Matyas,

Please check with the below link which might help you to accelerate further on the above requirement.

Hope this helps.

Best Regards,