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
someonesomeone 

Query for Task with Account and Contact information at the same time

I have a method to query for a specific Task object (or list of Tasks) based on the ID(s) of the object as follows:
        
webService static String getTasksByIds(String idsAsJson) {
   Set<Id> taskIds = (Set<Id>) JSON.deserialize(idsAsJson, Set<Id>.class); 
   List<Task> tasks = [SELECT Id, IsClosed, Status, Subject, Description, WhoId, Account.Name FROM Task WHERE Id IN : taskIds];
        
   Map<Id, Task> taskMap = new Map<Id, Task>();
   for (Task t : tasks) {
        taskMap.put(t.Id, t);
   }             
   return JSON.serialize(taskMap);
}

How can I also query for the Account object (with Cases, Opportunities, Contracts) and the Contact object all in the same query?

Thanks
pconpcon
Depending on what you are looking to get, there is some data you can get back.  For example, you can get the Name back for all objects, but you can't call Opportunity directly.
 
select IsClosed,
     Status,
     Subject,
     Description,
     WhoId,
     What.Name,  //Returns the name of the opp, account, etc
     What.Type, //Returns the object type name
     Account.Name,
     Account.MyCustomField__c
from Task
where Id in :taskIds

But adding something like Opportunity.Name will not work.