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
SURESH MINGANISURESH MINGANI 

SOQL to fetch Tasks that are assinged on a Particular Account

Hi
I need to display on vf  by SOQL to fetch Tasks that are assinged on a Particular Account
Best Answer chosen by SURESH MINGANI
Shamsi 110Shamsi 110
Apex Controller

//Controller code
//replace my whatid with yours.
public class accountTaskController{

List<Task> lstOfTasks = new List<Task>();
public string accountname {get;set;}


public List<Task> getAccountTasks()
{
    return lstOfTasks;
}


public PageReference getListOfTasks()
{
    
   lstOfTasks = [SELECT id,createddate, subject FROM Task WHERE whatId = '0017F00000Ge0WD'];
   return null;
    

}




}





//Vf Page
<apex:page controller="accountTaskController">
<apex:form>
<apex:pageBlock>
<apex:commandButton action="{!getListOfTasks}" value="Show Tasks"/>
</apex:pageBlock>
 <apex:pageBlock title="Tasks for Account">
 <apex:pageBlockTable value="{!AccountTasks}" var="task">
 <apex:column value="{!task.Id}"/>
 <apex:column value="{!task.subject}"/>
 <apex:column value="{!task.CreatedDate}"/>
 </apex:pageBlockTable>
 </apex:pageBlock>
 
 </apex:form>
</apex:page>



After Clicking Show task button here are the related tasks.

User-added image


Kindly mark my answer, if it helps you.

Thanks,
Hasan Shamsi

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
SELECT id, subject FROM Task WHERE whatId = 'YOur Account ID';
Shamsi 110Shamsi 110
Apex Controller

//Controller code
//replace my whatid with yours.
public class accountTaskController{

List<Task> lstOfTasks = new List<Task>();
public string accountname {get;set;}


public List<Task> getAccountTasks()
{
    return lstOfTasks;
}


public PageReference getListOfTasks()
{
    
   lstOfTasks = [SELECT id,createddate, subject FROM Task WHERE whatId = '0017F00000Ge0WD'];
   return null;
    

}




}





//Vf Page
<apex:page controller="accountTaskController">
<apex:form>
<apex:pageBlock>
<apex:commandButton action="{!getListOfTasks}" value="Show Tasks"/>
</apex:pageBlock>
 <apex:pageBlock title="Tasks for Account">
 <apex:pageBlockTable value="{!AccountTasks}" var="task">
 <apex:column value="{!task.Id}"/>
 <apex:column value="{!task.subject}"/>
 <apex:column value="{!task.CreatedDate}"/>
 </apex:pageBlockTable>
 </apex:pageBlock>
 
 </apex:form>
</apex:page>



After Clicking Show task button here are the related tasks.

User-added image


Kindly mark my answer, if it helps you.

Thanks,
Hasan Shamsi
This was selected as the best answer
SURESH MINGANISURESH MINGANI
Thank You  
Sumit Kumar Singh  & Shamsi