I'm trying to return a list of opportunities that fit the soql criteria but only need opportunities for which the user is part of the AccountTeamMember of the parent Account. For instance, user A is account team member for multiple accounts. The controller should return a list of opportunities from these accounts only.


public class OpportuityCustomController {
    public string uId{ get; set;}
    public string SFUrl{ get; set;}
    List<Opportunity> opps;
    public list<Opportunity>getOpportuityCustomController(){
      Map<Id, Id> oppMap = new Map<Id, Id>();
      for(AccountTeamMember atm :[Select a.UserId, a.AccountId From AccountTeamMember a where UserId = :uId]){
        opps = new List<Opportunity>();
      opps = [SELECT Id, Name, Effective_Date__c, Account.Name, AccountId, StageName 
               FROM Opportunity WHERE Notification_Date__c = TODAY AND 
                    StageName = 'Closed Won' AND 
                    AccountId IN :oppMap.keyset() LIMIT 50000];
      return opps;
  • The Limit 50000 is a bit of a joke here as your VF page won't be able to handle 50,000 Opportunities; VF iteration component can only process 1000 max.
    – cropredy
    Commented Mar 9, 2016 at 0:57

1 Answer 1


I believe the term for this type of query is inner-join sub-select.

List<Opportunity> records = [
    SELECT Id FROM Opportunity
    WHERE AccountId IN (
        SELECT AccountId FROM AccountTeamMember
        WHERE UserId = :UserInfo.getUserId()
system.assertEquals(1, Limits.getQueries(), 'Only one query should be consumed');
  • 1
    ..where userId =:UserInfo(..) ....
    – cropredy
    Commented Mar 9, 2016 at 0:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .