Need SOQL or SOSL query to return the Accounts Name having at least a Partner, and/or Contact and/or an Activity attached to it.

So far i am able to get the account name where owner ID is '00550000001bs0cAAA' but not able to drill down further on above criteria. (I have tried cross filter on report but it won't give me the desired result )

Query generated so far SELECT Name FROM Account WHERE OwnerID = '00550000001bs0cAAA'


In general, the way you would do this is to add a subquery in your where clause. For example, if you are interested only in Accounts that have at least one contact you would write something like:

[SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact)]

Unfortunately, you cannot use the OR operator in a sub select where statement, so something like this is not valid:

[SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact) OR Id IN (Select AccountId FROM Opportunity)]

You may run into some issues using the WhatId of a task/event, but I am not positive about that.

If this supposed to be executed within the context of Apex, then we have more tools in our disposal:

Set<Id> accountIds = new Set<Id>();
for (Opportunity opp : [Select AccountId FROM Opportunity ]) {
for (Contact con : [Select AccountId FROM Contact]) {
List<Account> accountsWithContactOrOpp = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];

Also, it is never a good idea to use a hard coded RecordId or RecordTypeId in a query in Apex. Instead, use something like Owner.Name = 'Mitch Spano'

| improve this answer | |

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.