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?
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?
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')];
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.