In Salesforce, we have standard objects such as tasks, attachments, notes, and events. One similarity between these objects is that any object can be the parent of these objects, rather than having a standard lookup to a single object type. For example, a whoid (parent) for the Case object record can be a lead or a contact.
In this Salesforce tutorial, I will explain how to query Tasks, Notes, and Attachments using SOQL.
SOQL Queries for Tasks, Notes, and Attachments by Object Type
In the SOQL queries below, we will see different scenarios for querying object tasks, notes, and attachments by object type.
SOQL query to list Tasks related to Leads in Salesforce
WhoIds are polymorphic, which means a WhoId is equivalent to a contact’s ID or a lead’s ID. When you have to list the task records with ‘whoId‘ (parent) as Contacts, you can execute the SOQL query below.
SOQL query:
SELECT Id, Who.Id, Who.Type FROM Task WHERE Who.Type = 'Contact'
Output:

In the above query, we have used the whoid to get the contacts related to the tasks, and if you want to get the Leads related to Tasks, then you can change Who.Type = ‘ Leads‘.
Where “whoid” is a person associated with an activity, like a contact or a lead, while “WhatId” refers to the business object the activity is related to, such as an account, opportunity, or case.
To query the WhatId of a task, you can run the following SOQL query.
SOQL query:
SELECT Id, What.Id, What.Type FROM Task WHERE What.Type = 'Account'
Output:
