0

I want to get all accounts present in a particular territory.

Can someone please help me out here to traverse and get the list of all accounts in a territory?

user3188849
  • 1
  • 1
  • 1

2 Answers2

1

The code should look like something like this

list<Group> map_group = [Select Id, RelatedId from Group where Type='Territory' AND RelatedId =: 'TerritoryId'];


List<AccountShare> lst_AccountShare = [Select Id, UserOrGroupId, AccountId from AccountShare where ( UserOrGroupId IN : map_group )AND RowCause IN ('Territory', 'TerritoryManual', 'TerritoryRule')];
Shimshon Korits
  • 1,189
  • 6
  • 10
  • I don't have an org with territories around but from what I remember it'll give only "direct links" (won't show accs from territories below)? There's a trick we used to store hierarchy of territory in a text(255) field on the record so we could query `WHERE Hierarchy__c LIKE 'Territory/Subterritory/SubSub%'` and that was fetching the ids of parent and all kids. Still - bit painful ;) – eyescream Jan 13 '14 at 06:19
0

Check these questions on http://salesforce.stackexchange.com to start. If you'll be still stuck - post the code you've tried so far?

You might need recursive querying on AccountShare to find out accounts that are linked to exactly that territory + accounts linked to all subteritories... If you don't feel like it - a somewhat crazy idea would be create a report with saved territory level and then simply fetch the results of that report using recenly introduced Analytics API.

Community
  • 1
  • 1
eyescream
  • 18,088
  • 2
  • 34
  • 46