I need to get the Account owners. In our case they are the users assigned to territories.
Below is my approach.
- Query the Accounts
Query the AccountShare object for above accounts select Id, AccountId, UserOrGroupId, RowCause from AccountShare where AccountId IN :accounts and RowCause In ('TerritoryManual','Territory')
Query Group object based on UserOrGroupId from above query result select Id, RelatedId from Group where Id IN AccountShare.RelatedId
Query UserTerritory based on the territoryId result form above query select Id, UserId, TerritoryId from UserTerritory where TerritoryId IN :Group.TerritoryId
My problem is I don't know how to get the mapping between Account and UserId.
Also tried combining AccountShare and Group query in single SOQL but it's not working.(UserOrGroupId is a foreign key in AccessShare object but not sure how to traverse the relationship)
Any help is appreciated!