[SalesForce] Activities on Leads, Opportunities, and Accounts SOQL

I'm trying to get SOQL's for Activities (workbench was no help).

I'm looking at the three prebuilt reports in Salesforce:

Activities on Leads, Activities on Accounts, and Activities on Opportunitites

and would like to see "open & completed activites", both "Events & Tasks", as well as a date frame for Activity Date in THIS_YEAR.

My best guess are for eg 'Activities on Leads' is:

SELECT Report_Country_L__c, (SELECT Owner.Name,ActivityDate,Status,istask,TaskSubtype,
    Subject FROM Task WHERE ActivityDate = This_Year) FROM Lead

But it does not work!

Any idea?

Best Answer

The object you should query is OpenActivity and ActivityHistory

So your query should be

SELECT Report_Country_L__c,
(SELECT Owner.Name,ActivityDate,Status,istask,ActivitySubtype, Subject FROM OpenActivities WHERE ActivityDate = This_Year),
(SELECT Owner.Name,ActivityDate,Status,istask,ActivitySubtype, Subject FROM ActivityHistories WHERE ActivityDate = This_Year)
FROM Lead

EDIT:

If you want to retrieve both Events and Tasks without considering whether its Open or Completed, you can go with Devendra's approach. If your consideration if to get only Open or Completed Events and Tasks, then you can go with the above approach. In this case you should only include either ActivityHistories or OpenActivities subquery.

Hope it helps.

Related Topic