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
Chris Walters 9Chris Walters 9 

downward SOQL query

version 47

SELECT Id, Name, (SELECT Last_Contact_Method__c FROM Contacts ) FROM Account

SELECT Id, Status, WhatId FROM Task
where Task.WhatId =  account.Id from the above query

how would I nest the downward SOQLs to achieve
Select Id, Status, Email_Direction__c, (SELECT Name, (SELECT Last_Contact_Method__c FROM Contacts ) FROM ???? ) FROM Task

I've tried some variations of WhatId like
  • What
  • Whats
  • WhatId
  • WhatIds
  • What__r
  • Whats__r
to no avail. 

Le NguyenLe Nguyen
You cannot do the nested SOQL with WhatId because Whatid is ID of related Account,Opp, Camaign, Case or custom object.
AnudeepAnudeep (Salesforce Developers) 
Hi Chris, 

SOQL statements cannot query aggregate relationships more than 1 level away from the root entity object meaning if you were to execute a SOQL query like this, it gives you an error 
Select id, (Select Id, FirstName, LastName, (Select Id, WhoId FROM Tasks) FROM Contacts) from Account

 Contact and task have a parent-child relationship. The same applies to Account so the following queries will work 
Select Id, FirstName, LastName, (Select Id, WhoId FROM Tasks) FROM Contact
SELECT Id, Name, (SELECT Id, whoId Subject FROM Tasks) FROM Account WHERE CreatedDate = TODAY

You should use separate queries to get the result you are looking for. See Using Relationship Queries to learn more


Chris Walters 9Chris Walters 9
Good answers! I will try them shortly, THX