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
Thomas Reinman 16Thomas Reinman 16 

Help with SOQL Query On Polymorphic Field (WhatId - Task)

I'm trying to query tasks related to opportunity, provided the context that the user is on the opportunity record. This query will grab that Opportunity ID and display the related tasks in a visualforce page on the layout.

Before you ask what the purpose is, this SOQL query doesn't include all of the filters on Tasks yet; first I just need to get it working for any related tasks. 

The apex class is:
 
public class discoQuery{

//VARIABLES

public Opportunity opp {get; set;}
ApexPages.StandardController sController;
public Map<ID,Task> tMap {get; set;}
    
//CONSTRUCTOR
public discoQuery(ApexPages.StandardController controller){
    sController = controller;
    opp = (Opportunity)controller.getRecord();
    }
    
public List<Task> tList{
    get{
        if(tMap == null){
            tMap = new Map<ID, Task>([SELECT Id, Subject, Activity_Type__c, Outcome__c, Status, OwnerId FROM Task WHERE What.Type IN ('Opportunity') AND WhatId =: ([SELECT Id FROM Opportunity WHERE Id =: opp.Id]) AND IsDeleted = FALSE ORDER BY CreatedDate LIMIT 1000 ALL ROWS]);
        }
            return tMap.values();
        }
    }
}

Visualforce:
<apex:page standardController="Opportunity" extensions="discoQuery" lightningStylesheets="true">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!tList}" var="t">
            <apex:outputField value="{!t.Subject}"/>
            <apex:outputField value="{!t.Activity_Type__c}"/>
            <apex:outputField value="{!t.Status}"/>
            <apex:outputField value="{!t.Outcome__c}"/>
            <apex:outputField value="{!t.OwnerId}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>

Any thoughts on why this query isn't returning any records?

Thanks​​​​​​​
 
Best Answer chosen by Thomas Reinman 16
Thomas Reinman 16Thomas Reinman 16
Resolved with the following code. The Visualforce PageBlockTable was the issue, so I used an apex:DataTable instead. 
 
public class discoTaskOnLightning{

    Public List<Task> taskList;
    Public List<Task> discos;
    String currentRecordId; 

    public discoTaskOnLightning(ApexPages.StandardController controller) {
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        taskList = [SELECT Id, Subject, Activity_Type__c, ActivityDate, Outcome__c, Status, OwnerId, WhatId, WhoId FROM Task WHERE WhatId =: currentRecordId AND Activity_Type__c = 'Discovery Call' AND IsDeleted = FALSE ORDER BY CreatedDate LIMIT 1000];
        }
   
   public List<Task> getDiscos(){
       if(discos == null){
           discos = [SELECT Id, Subject, Activity_Type__c, ActivityDate, Outcome__c, Status, OwnerId, WhatId, WhoId FROM Task WHERE WhatId =: currentRecordId AND Activity_Type__c = 'Discovery Call' AND IsDeleted = FALSE ORDER BY CreatedDate LIMIT 1000];
           
           dateOfActivity = DATEVALUE(ActivityDate);
           
           if(TaskList.size() == 0)
            {
                Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No past discovery calls to show.'));
            } 
            
           } return discos;
        }
}
 
<apex:page standardController="Opportunity" extensions="discoTaskOnLightning" lightningStylesheets="true">
<apex:pageMessages ></apex:pageMessages>

<apex:outputPanel >   
<apex:dataTable value="{!discos}" var="t" styleClass="tableClass">
    <!--<apex:facet name="header"> Discovery Calls from the Past </apex:facet>-->
    
    <br/><br/><br/>
    
    <apex:column headervalue="Task">
        <apex:outputLink value="/{!t.id}"> {!t.Activity_Type__c}</apex:outputLink>
    </apex:column>
    
    <apex:column headerValue="Status">
        <apex:outputText value="{!t.Status}"/>
    </apex:column>
    
     <apex:column headerValue="Outcome">
        <apex:outputText value="{!t.Outcome__c}"/>
     </apex:column>
    
    <apex:column headerValue="Date Completed">
        <b><apex:outputText value="{!t.ActivityDate}"/></b>
     </apex:column>
    
</apex:dataTable>
</apex:outputPanel> 
</apex:page>

Not too pretty but it works...

User-added image
 

All Answers

Thomas Reinman 16Thomas Reinman 16
I've tried a slightly different approach, and still no Tasks are showing up. 
public class discoTaskOnLightning{

    Public List<Task> taskList {get;set;}
    String currentRecordId; 

    public discoTaskOnLightning(ApexPages.StandardController controller) {
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        taskList = [SELECT Id, Subject, Activity_Type__c, Outcome__c, Status, OwnerId FROM Task WHERE WhatId =: currentRecordId AND IsDeleted = FALSE ORDER BY CreatedDate LIMIT 1000];

         if(TaskList.size() == 0)
            {
                Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
            } 
        } 
}

 
Thomas Reinman 16Thomas Reinman 16
Resolved with the following code. The Visualforce PageBlockTable was the issue, so I used an apex:DataTable instead. 
 
public class discoTaskOnLightning{

    Public List<Task> taskList;
    Public List<Task> discos;
    String currentRecordId; 

    public discoTaskOnLightning(ApexPages.StandardController controller) {
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        taskList = [SELECT Id, Subject, Activity_Type__c, ActivityDate, Outcome__c, Status, OwnerId, WhatId, WhoId FROM Task WHERE WhatId =: currentRecordId AND Activity_Type__c = 'Discovery Call' AND IsDeleted = FALSE ORDER BY CreatedDate LIMIT 1000];
        }
   
   public List<Task> getDiscos(){
       if(discos == null){
           discos = [SELECT Id, Subject, Activity_Type__c, ActivityDate, Outcome__c, Status, OwnerId, WhatId, WhoId FROM Task WHERE WhatId =: currentRecordId AND Activity_Type__c = 'Discovery Call' AND IsDeleted = FALSE ORDER BY CreatedDate LIMIT 1000];
           
           dateOfActivity = DATEVALUE(ActivityDate);
           
           if(TaskList.size() == 0)
            {
                Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No past discovery calls to show.'));
            } 
            
           } return discos;
        }
}
 
<apex:page standardController="Opportunity" extensions="discoTaskOnLightning" lightningStylesheets="true">
<apex:pageMessages ></apex:pageMessages>

<apex:outputPanel >   
<apex:dataTable value="{!discos}" var="t" styleClass="tableClass">
    <!--<apex:facet name="header"> Discovery Calls from the Past </apex:facet>-->
    
    <br/><br/><br/>
    
    <apex:column headervalue="Task">
        <apex:outputLink value="/{!t.id}"> {!t.Activity_Type__c}</apex:outputLink>
    </apex:column>
    
    <apex:column headerValue="Status">
        <apex:outputText value="{!t.Status}"/>
    </apex:column>
    
     <apex:column headerValue="Outcome">
        <apex:outputText value="{!t.Outcome__c}"/>
     </apex:column>
    
    <apex:column headerValue="Date Completed">
        <b><apex:outputText value="{!t.ActivityDate}"/></b>
     </apex:column>
    
</apex:dataTable>
</apex:outputPanel> 
</apex:page>

Not too pretty but it works...

User-added image
 
This was selected as the best answer