+ Start a Discussion
TehNrdTehNrd 

How to replicate a "My Account Team and My Accounts" query?

When running a report and if Account Teams are enabled you have an option to search "My Account Team and My Accounts". This returns accounts in which you are listed on the sales team or you are the account owner. Belwo is the SOQL and error I am recieving:

 

List<Account> acc = [SELECT Id FROM account WHERE Id IN (SELECT accountId FROM accountteammember WHERE UserId = :UserInfo.getUserId()) OR ownerid = :UserInfo.getUserId()];

 Error: Semi join sub-selects are not allowed with the 'OR' operator

 

Is this a dead end? Any ideas how this could be accomplished with SOQL....or maybe even without SOQL.

 

Thanks,

Jason

 

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower

Jason,

 

I suppose you could split it into two SOQLs and do a union across them. May need to load it into a Set if you also want eliminate dups between the two lists.

 

 

List<Account> acc = [SELECT Id FROM account WHERE Id IN (SELECT accountId FROM accountteammember WHERE UserId = :UserInfo.getUserId())] 

List<Account> userOwnedAcc = [SELECT Id FROM account WHERE ownerid = :UserInfo.getUserId()];
acc.addAll(userOwnedAcc);

Best,
Ram

All Answers

ForcepowerForcepower

Jason,

 

I suppose you could split it into two SOQLs and do a union across them. May need to load it into a Set if you also want eliminate dups between the two lists.

 

 

List<Account> acc = [SELECT Id FROM account WHERE Id IN (SELECT accountId FROM accountteammember WHERE UserId = :UserInfo.getUserId())] 

List<Account> userOwnedAcc = [SELECT Id FROM account WHERE ownerid = :UserInfo.getUserId()];
acc.addAll(userOwnedAcc);

Best,
Ram
This was selected as the best answer
TehNrdTehNrd

Thats a good idea. In the second query I could probably do something like this to remove the dupes:

 

List<Account> userOwnedAcc = [SELECT Id FROM account WHERE ownerid = :UserInfo.getUserId() AND Id NOT IN :acc];
acc.addAll(userOwnedAcc);
ForcepowerForcepower

Yep - even better. No need for a Set etc.

Ram