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:

In this SOQL query, we have retrieved the accounts related to the task using Whatid.
SOQL Query To Return Any Open Tasks Assigned To a User
To retrieve the information about the accounts related to tasks, the query below will return the account information you need for any open tasks assigned to ‘User_Name.’
SOQL query:
SELECT Id, AccountId, Account.Name FROM Task WHERE Owner.Name = 'User Name' AND isClosed = false Replace the username with the name of the person for whom you want to find the open tasks.
Output:

As shown in the output, the query returns a list of open tasks for a specific user.
SOQL Query to fetch Events related to Accounts in Salesforce
In the SOQL query below, we will retrieve account-related events and their details, including ID, Subject, StartDateTime, and EndDateTime.
SOQL query:
SELECT Id, Subject, StartDateTime, EndDateTime, WhatId
FROM Event
WHERE WhatId IN (SELECT Id FROM Account)Output:

In the output, we will obtain other details related to the Event object, and in the WhatId, we will find the ID of the accounts linked to the events.
SOQL query to fetch a list of Notes where the parent is a specific type
In the SOQL query below, we will retrieve a list of notes where the parent object is of a specific type. In this example, we will get the list of Notes where the parent is Opportunity.
SOQL query:
SELECT Id, Parent.Id, Parent.Type FROM Note WHERE Parent.Type = 'Opportunity'Output:

In the output of the above SOQL query, we can see that the notes related to the opportunities are retrieved. Similarly, when you need to query notes related to an account object, change the Parent.Type= ‘Account’.
SOQL query to get Attachments from a specific object
To retrieve the attachment records associated with a specific object, we can execute the SOQL query below. In the example below, we will get the attachments from the Account object.
SOQL query:
SELECT Id, Name, ParentId FROM Attachment WHERE Parent.Type = 'Account'Output:

This way, we can query the attachment records in Salesforce with their parent object ID.
SOQL Query to fetch the list of Completed Tasks related to the object
In the SOQL query below, we will fetch the completed tasks related to the Account object.
SOQL query:
SELECT Id, Status, Subject, WhatId FROM Task WHERE What.Type = 'Account' AND Status = 'Completed'Output:

In the output of the executed SOQL query, we can see the Status of the retrieved Tasks as completed.
SOQL query to get the tasks related to Contacts and Leads
When we have to query tasks linked to either Leads or Contacts, we can execute the following SOQL query.
SOQL query:
SELECT Id, Subject, WhoId, Who.Type FROM Task WHERE Who.Type IN ('Lead', 'Contact')This way, you can query the task records from both contact and lead in a single SOQL query.
SOQL Query to Retrieve Attachments with Specific Keywords in the Name
You can use the following query to find attachment records with a specific keyword in their names (e.g., “Report“). This query is helpful when you don’t know the object; with this, you can find both the attachment and its parent object.
SOQL query:
SELECT Id, Name, ParentId FROM Attachment WHERE Name LIKE '%Report%'Output:

In the output of the SOQL query, we can see that we have fetched the attachment record with the keyword ‘doc,’ which we have specified in the SOQL query.
Conclusion
In this Salesforce tutorial, we have learned about various SOQL queries to retrieve data from Salesforce standard objects, such as Tasks, Notes, Attachments, and Events.
In the above examples, we have covered various scenarios where we have queried tasks related to contacts, leads, and accounts, retrieved events linked to accounts, and retrieved notes for specific parent objects, such as opportunities.
By following these query methods, you can fetch records related to tasks, events, notes, and attachments more efficiently.
You may also like to read:
- Query SOQL NOT LIKE Operator in Salesforce
- Salesforce SOQL Logical Operators
- What is the Difference between SOQL and SOSL in Salesforce
- Query to Count Child Records in Salesforce SOQL
- Alias Notations in Salesforce SOQL

Abhijeet is a skilled Salesforce developer with experience in developing and integrating dashboards, data reports, and Salesforce applications. He is also skilled at optimizing processes and flow automation processes, coding, and executing complex project architecture. Read more about us | LinkedIn Profile.