4

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?

4

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.

  • Since he mentions Events, are you certain he wants isTask = true to be part of his query? – crmprogdev Aug 5 '16 at 12:19
  • @crmprogdev I reread the question and removed the istask=true from the subquery in my answer. Thanks for pointing that out Cal . – Vigneshwaran G Aug 5 '16 at 12:21
3

You need to use the correct relationship name to access tasks from the Lead parent object. That relationship name is usually slightly different from the Object name.

In this case, the relationship name for tasks is Tasks. So change your query to:

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

A further general explanation can be found in the docs: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_parent_child.htm#sforce_api_calls_soql_relationships_parent_child

3

You need to query on Event and Task object to retrieve Activities for Lead object. You can verify the Relationship name using Workbench --> Info --> Standard and Custom Object -> Select and Object for which Relationship to be verified

List<Lead> leadList = [Select id, (Select id from Events), (Select Id from Tasks) from Lead];
System.debug('::'+leadList);

enter image description here

2

Activity is a "catch all" term that includes Event and Task. You need to query both objects to get all Activities. They each have a different date fields that you'll need to consider when building your query. I suggest you take a look at the Object Reference to get the correct relationships. Once you do, I believe you'll end up with an AggregateResult query. You can also look to see if they can be queried together using Activity, but off hand, I don't think so.

1

You can do something like this

Select Id, What.type, who.type from task
Select Id, What.type, who.type from Event

using these method and two query you will get all task and event for All three object or any other object.

Note: the 50000 records limit in SOQL will still apply in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.