I need to get the Account owners. In our case they are the users assigned to territories.

Below is my approach.

  1. Query the Accounts
  2. Query the AccountShare object for above accounts select Id, AccountId, UserOrGroupId, RowCause from AccountShare where AccountId IN :accounts and RowCause In ('TerritoryManual','Territory')

  3. Query Group object based on UserOrGroupId from above query result select Id, RelatedId from Group where Id IN AccountShare.RelatedId

  4. 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!



You will need to create a series of maps, one at each step, to create the linkage. First a map of accounted to groupid - Map. The one item that could be tricky would be the user to group mapping. Since more than one user could be in the group, the final map might need to be a list map - Map>

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.