function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
moverdorfmoverdorf 

How To Query To Get Results Shown In Open Activities form?

Does anyone know what the SOQL query would look like to create the results shown in the Open Activities section of the Salesforce inteface?

I have tried to replicate it, but don't know how to get a list of Open Acivities for a contact, if the Open Activity doesn't belong directly to the Contact's Account.

For example, for my case, in the Open Activities section I see:

Subject        Name           Related To
AAA            John Smith    John Smith
BBB            John Smith     John Smith
CCC            John Smith     CM-137 (Client Meeting)

In my SOQL I can't get the CCC John Smith CM-137 row to be returned in my query, because the Task is NOT related to my Contact John Smith.

I have no idea how SF is doing this, can anyone help with a SOQL query or some code that would accomplish this? Thanks



For example, if a task's what.id is NOT for the contact but for something else, like a Client Meeting, I still want this task to be inlcuded in my query for the
ShashankShashank (Salesforce Developers) 
You can use the whoId instead. A whoId stores the contact that an activity belongs to. The "Related To" field is the "WhatId" field and the "Name" field is the "WhoId" field internally in the task or event object.

So, the SOQL may look something like this:

Select Subject, Who.Name, What.Name from task where who.Name = 'John Smith'
SarfarajSarfaraj
You do not need the name to display it. Here is one example,
<apex:page standardController="Contact">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!Contact.OpenActivities}" var="a">
            <apex:column value="{!a.Subject}"/>
            <apex:column value="{!a.WhoId}"/>
            <apex:column value="{!a.IsTask}"/>
            <apex:column value="{!a.ActivityDate}"/>
            <apex:column value="{!a.Status}"/>
            <apex:column value="{!a.Priority}"/>
            <apex:column value="{!a.OwnerId}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

This one is without any controller and soql. If you need to implement using soql, here is it,
 
SELECT (SELECT Subject, WhoId, IsTask, ActivityDate, Status, Priority, OwnerId FROM OpenActivities) FROM Account
--Akram
moverdorfmoverdorf
The SOQL below is what I had already tried:
SELECT (SELECT Subject, WhoId, IsTask, ActivityDate, Status, Priority, OwnerId FROM OpenActivities) FROM Account

The problem with this, is that it will only bring back Open Activities that belong to the related Account. My Issue is, I need to bring back any Open Activities that may or may not be directly related to the account. So I need to NOT use the Account object in the query, but some other way to get Open Activities.
moverdorfmoverdorf
Shasank, Using your SOQL
Select Subject, Who.Name, What.Name from task where who.Name = 'John Smith'

Results again in the first 2 rows of my example coming back and NOT the third... I tested it in my org I believe it is because the What.id is different than the other two rows that are retrieved.