In Salesforce, activities are used to track all the actions and activities we have done with accounts, leads, and contacts related to them in our Salesforce org. In Salesforce, activities have a polymorphic relationship that allows them to relate to other database records, which use two ID fields, WHOID and WHATID.
In this Salesforce tutorial, I will explain the method to query activity related to fields, objects, and activity history using SOQL in Salesforce.
Query Activity With SOQL in Salesforce
To query activities in Salesforce using SOQL, we can use the Task and Event objects, which represent the activity records in Salesforce.
The query below is an example of how we can query activities related to the Task object.
SELECT Id, Subject, WhatId, WhoId, Status, Priority, ActivityDate FROM TaskIn the above query, the WhatId is the related object that can be Account or Opportunity, and WhoId is related to Contact or Lead.
Output:

In a similar way, we can query the activities of the Event object using the following SOQL query.
SELECT Id, Subject, StartDateTime, EndDateTime, Location, Description FROM EventOutput:

This way, we can query the activities related to Tasks and Events in Salesforce using the above SOQL query methods.
Query Activity of related objects using SOQL
The activities have polymorphic relationships, and it is difficult to query the parent related to fields besides its ID & Name because it could refer to different parent objects.
For example, if I want to query the task related to the account object and the billing state of the related Account, then ideally, the SOQL query should be in the following way.
SELECT Id,
WhatId,
What.Name,
What.BillingState
FROM Task
WHERE WhatId In (SELECT Id
FROM Account)
Now, to avoid this, we will use the “TYPEOFF” operator that queries the Account and its related fields.
SELECT Id,
WhatId,
TypeOf What
WHEN Account THEN BillingState, BillingCity
END
FROM Task
WHERE WhatId In (SELECT Id
FROM Account)Output:

As we can see, the related accounts with the fields Billing State and Billing City are queried in the results.
Accessing Related To Fields In Apex
Using the above SOQL methods, we can query the related fields with activities, but we can’t directly access the polymorphic fields in Apex code.
We can only access the ID and the name in the Apex code, but we can indirectly access the fields using the get function on the related record.
You can follow the method below to access the related fields in Apex.
List<Task> tasks =
[SELECT Id,
WhatId,
TypeOf What
WHEN Account THEN BillingState, BillingCity
END
FROM Task
WHERE WhatId In (SELECT Id
FROM Account)];
for (Task t : tasks) {
SObject whatRecord = t.What;
String billingState = (String) whatRecord.get('BillingState');
String billingCity = (String) whatRecord.get('BillingCity');
system.debug(' whatRecord.BillingState:' + billingState);
system.debug(' whatRecord.BillingCity:' + billingCity);
}Output:

This way, we can query the related fields with activities having polymorphic relationships in Apex.
SOQL Query to Get Activity History for a Record
In Salesforce, to retrieve the Activity History for a specific record using SOQL, we can query the Task and Event objects, as these are the standard objects Salesforce uses for activities.
But, when we have to query specific records of other objects with activity history, such as the activity history of accounts, leads, and opportunities, we can’t fetch it from the tasks and events because it will only return the activity history of tasks related to specific objects.
To get the activity history of all account records, we can execute the following SOQL query.
SELECT Id, (SELECT Id, Subject, CreatedDate FROM ActivityHistories) FROM AccountIn the output, you will find all the history of activity related to the account records.

If you want to query the activity history of any specific object records then you can add the ID to query in the following way.
SELECT Id, (SELECT Id, Subject, CreatedDate FROM ActivityHistories) FROM Account WHERE Id = '0015i00000v5rIRAAY'Output:

This way, we can track the activity history of Salesforce standard and custom objects using SOQL.
Conclusion
In this Salesforce tutorial, we have learned about querying the fields related to activity, activity related to the objects, and related to fields (WhoId) in Salesforce using the SOQL queries.
We also learned the method to access the activities of the related fields in the Apex code, and then we queried the history of activities related to the Account. By specifying the ID, we queried the activity history of specific account records.
You may also like to read:
- Salesforce SOQL and SOSL Limits for Search Queries
- Find Deleted Records in Salesforce using SOQL
- Query to Count Child Records in Salesforce SOQL
- Query Validation Rule in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.