代码应该看起来像这样
1)
Map<Id,UserTerritory> UserTerritoryCurrentUserMap = new Map<Id,UserTerritory>([Select u.UserId, u.TerritoryId, u.IsActive, u.Id From UserTerritory u Where u.isActive=true and u.userId =: userId]);
2)
set<Id> TerritoryIdSet = new set<Id>();
for (UserTerritory ut:UserTerritoryCurrentUserMap.values()) {
TerritoryIdSet.add(ut.TerritoryId);
}
3)
list<Group> map_group = [Select Id, RelatedId from Group where Type='Territory' AND RelatedId IN : TerritoryIdSet];
4)
List<AccountShare> lst_AccountShare = [Select Id, UserOrGroupId, AccountId from AccountShare where ( UserOrGroupId IN : map_group OR UserOrGroupId =:userId )AND RowCause IN ('Territory', 'TerritoryManual', 'TerritoryRule')];