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
Andreas ChristidesAndreas Christides 

query CaseId on Tasks to retieve all Tasks linked to a specific Case.

Hi all, 

I want to create a Lightning Component that would retrieve Tasks linked to a specific Case. 

Here is the code I got from a component that retrieves Tasks based on the UserId : 
<aura:component controller="tasksController" access="global" implements="flexipage:availableForAllPageTypes,force:appHostable">
    <aura:attribute name="heading" type="String" access="global" default="Mes Tâches"/> 
    <aura:attribute name="tasks" type="Task[]" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <div class="My-slds-card">
    <article class="slds-card slds-card--narrow">
      <div class="slds-card__header slds-grid">
        <header class="slds-media slds-media--center slds-has-flexi-truncate">
           <div class="slds-media__body">
                <span class="slds-text-heading--small">{!v.heading}</span>
      <div class="slds-card__body">
        <div class="slds-card__body--inner">
                <ui:inputSelect aura:id="selection" class="slds-picklist" change="{!c.changeFilter}">
                    <ui:inputSelectOption text="AllOpen" label="Toutes en cours"/>
                    <ui:inputSelectOption text="Overdue" label="En retard"/>
                    <ui:inputSelectOption text="Today" label="Aujourd'hui" />
                    <ui:inputSelectOption text="TodayOverdue" label="Aujourd'hui + en retard"/>
                    <ui:inputSelectOption text="Tomorrow" label="Demain"/>
                    <ui:inputSelectOption text="NextSevenDays" label="7 prochains jours" value="true"/>
                    <ui:inputSelectOption text="NextSevenDaysOverdue" label="7 prochains jours + en retard"/>
                    <ui:inputSelectOption text="ThisMonth" label="Mois en cours"/>
        <div class="slds-card__body--inner">
        <aura:iteration items="{!v.tasks}" var="task">
          <div class="slds-tile">
            <h3 class="slds-truncate" title="Related Record Title 1">
                <a href="{! '#/sObject/' + task.Id + '/edit'}"  class="ui-replce-style">
                    <lightning:icon iconName="standard:task" size="small" alternativeText=""/> &emsp;{!task.Subject}
                <ul class="list">
                         {!task.Type} | {!task.Status} | <ui:outputDate value="{!task.ActivityDate}"/> <br/>
        <div class="slds-card__footer"><a href="{! '#/sObject/Task/home'}"  class="ui-replce-style">View All</a></div>

    doInit : function(component) {
        var action = component.get("c.MyTask");   

        // get the value for Label field, if empty then default it to 'My Tasks'
        var isEmptyHeading = $A.util.isEmpty(component.get("v.heading"));
        if (isEmptyHeading) {
            component.set("v.heading","Mes Tâches");

        action.setCallback(this, function(data) {
            component.set("v.tasks", data.getReturnValue());
        var action = component.get("c.TaskByFilter");
        var selectCmp = component.find("selection");
        var selectVal = selectCmp.get("v.value");
        console.log('Selected Value '+selectVal);
          "fltr": selectVal
        action.setCallback(this, function(data) {
            component.set("v.tasks", data.getReturnValue());

public class tasksController {
    public static List<Task> MyTask() {
        if (!Schema.SObjectType.Task.fields.subject.isAccessible()){
            return NULL;
        return [SELECT Id, ActivityDate, Priority, Status, Type, Subject FROM Task WHERE OwnerId = :UserInfo.getUserId() and ActivityDate = NEXT_N_DAYS:7 and IsClosed=false limit 20 ];
    public static List<Task> TaskByFilter(string fltr) {
        if (!Schema.SObjectType.Task.fields.subject.isAccessible()){
            return NULL;
        List <Task> taskList = new List <Task> ();
        string query='',whereClause='';
        if(fltr =='AllOpen'){
        }else if ( fltr == 'ThisMonth'){
            whereClause='and ActivityDate = THIS_MONTH';
        else if( fltr == 'Overdue'){
            whereClause='and ActivityDate < TODAY';
        else if(fltr == 'Today'){
            whereClause='and ActivityDate = TODAY';
        else if(fltr == 'TodayOverdue'){
            whereClause='and ActivityDate <= TODAY';
        else if(fltr == 'Tomorrow'){
            whereClause='and ActivityDate = TOMORROW';
        else if(fltr == 'NextSevenDays'){
            whereClause='and (ActivityDate = NEXT_N_DAYS:7 OR ActivityDate = TODAY)';
        }else {
            whereClause='and ActivityDate <= NEXT_N_DAYS:7';
        query = 'SELECT Id, ActivityDate,Priority,Status,Subject FROM Task WHERE (ownerId = \'' + UserInfo.getUserID() + '\' and IsClosed=false ) '+whereClause+' limit 100';
        taskList = Database.query(query); 
        return taskList;
When trying to query the CaseId on the SOQL part of the Apex Class it doesn't return anything. I'm sure I did it the wrong way as I'm quite green to Apex and coding. 

Thanks a lot.