function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bujjibujji 

SOQL query for Task,Opportunity and Account

Hi Guys,

 

I need a query to bring all the tasks of Account and tasks of Opportunities which related to the Account.

 

I have written a query which is bringing all tasks of Opprotunities of related Account, see the below query

List<Task> taskList = [select Id,OwnerId,whoId,whatId,Subject,ActivityDate,Priority,Status From Task
                       where   whatid IN (select Id From Opportunity where Account.Id ='0019000000DOsVO')
                         ];

along with this i need the task of Account also. How to do it.
give me some suggestion. Urgent please.

 

Thanks,

Bujji

georggeorg

Get account ids into a list named AccountIdslst and query all opportunities for that account by

 

list<Ids>OpportunityIdslst = [Select Id from opportunity where accounid in :AccountIdsSet ];

 

Now combine all the ids, accountids+opportunityIds

 

aet<Id> AllIds = New aet<Id>()

AllIds.addAll(AccountIdslst);

AllIds.addAll(OpportunityIdslst);

 

Now query Tasks for both opportunity and accounts by

 

List<Task> allTasks = [select id, other fields from task where whatid in :AllIds]

 

Hope this helps