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
Jonathan Wolff 7Jonathan Wolff 7 

Link the columns in Task component to the displayed Task

Hello, I build an Task component. I want the link on the subject to lead to the task. I only achived to show the ID, but it doesnt lead to the task. In addition I would like to change it, that only my tasks are shown instead of all.
Could you look over my code and tell me how to fix this problem?User-added image

<aura:component controller="TaskController" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" >
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="newTask" type="Task" default="{'sobjectType':'Task'}"/>
    <aura:attribute name="tasks" type="Task[]"/>
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <aura:attribute name="mycolumns" type="List"/>
    <lightning:card iconName="standard:task" title="Tasks">
        <div class="slds-card__body slds-card__body_inner">
        </div>
        <div>
            <aura:if isTrue="{!not(empty(v.tasks))}">
                <lightning:datatable data="{!v.tasks }" 
                         columns="{!v.mycolumns }" 
                         keyField="Id"
                         hideCheckboxColumn="true"/>
                <aura:set attribute="else">
                    <div Style="text-align : center"> " There are no Tasks currently "
             </div>
                </aura:set>
            </aura:if>
        </div>
    </lightning:card>
</aura:component>

Controller:
({
    doInit: function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Id', fieldName: 'Id', type: 'url', 
            typeAttributes: {label: { fieldName: 'Id' }, target: '_blank'}},
            
            {label: 'Subject', fieldName: 'Id', type: 'url',
            typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},
            
            {label: 'Activity Date', fieldName: 'ActivityDate', type: 'text'},
        ]);
        var action = component.get("c.loadTasks");
        var whatId = component.get("v.recordId");
        action.setParams({
            "recordId":whatId
        });
            
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var records =response.getReturnValue();
                records.forEach(function(record){
                   
                    record.Subject= record.Subject;
                    record.ActivityDate= record.ActivityDate;
                });
                component.set("v.tasks", records);
            }
        });
        $A.enqueueAction(action);
    },
    createNewTask : function(component, event, helper) {
        var action = component.get("c.saveTask");
        var newTask = component.get("v.newTask");
        action.setParams({
            "task": newTask
        });
        action.setCallback(this,function(response){
            var state = response.getState();
   
            if(component.isValid() && state === "SUCCESS"){
                var items = component.get("v.tasks");
                items.push(response.getReturnValue());
                component.set("v.tasks",items);
            }
            else{
                console.log("Failed with state "+state);
            }
        });
        $A.enqueueAction(action);
    }
})

Apex:
public with sharing class TaskController {
  @AuraEnabled
  public static List<Task> loadTasks(Id recordId){
    return[SELECT Subject, ActivityDate FROM Task WHERE WhatId=:recordId And ActivityDate>= TODAY AND ActivityDate  <= Next_N_Days:3];
  }
  @AuraEnabled
  public static Task saveTask(Task task){
    upsert task;
    return task;
   }
}
Best Answer chosen by Jonathan Wolff 7
CharuDuttCharuDutt
Hii Jonathan Wolff
I've Made Some Changes in Your js.
({
    doInit: function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Id', fieldName: 'Id', type: 'url', 
            typeAttributes: {label: { fieldName: 'Id' }, target: '_blank'}},
            
            {label: 'Subject', fieldName: 'SubjectName', type: 'url',
            typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},
            
            {label: 'Activity Date', fieldName: 'ActivityDate', type: 'text'},
        ]);
        var action = component.get("c.loadTasks");
        var whatId = component.get("v.recordId");
        action.setParams({
            "recordId":whatId
        });
            
            
            
            
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var records =response.getReturnValue();
                records.forEach(function(record){
                   
                    record.SubjectName = '/'+record.Id;
                    record.ActivityDate= record.ActivityDate;
                });
                component.set("v.tasks", records);
            }
        });
        $A.enqueueAction(action);
    },
    createNewTask : function(component, event, helper) {
        var action = component.get("c.saveTask");
        var newTask = component.get("v.newTask");
        action.setParams({
            "task": newTask
        });
        action.setCallback(this,function(response){
            var state = response.getState();
   
            if(component.isValid() && state === "SUCCESS"){
                var items = component.get("v.tasks");
                items.push(response.getReturnValue());
                component.set("v.tasks",items);
            }
            else{
                console.log("Failed with state "+state);
            }
        });
        $A.enqueueAction(action);
    }
})

Please Mark it As best Answer if it Helps.
Thank You!

All Answers

CharuDuttCharuDutt
Hii Jonathan Wolff
I've Made Some Changes in Your js.
({
    doInit: function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Id', fieldName: 'Id', type: 'url', 
            typeAttributes: {label: { fieldName: 'Id' }, target: '_blank'}},
            
            {label: 'Subject', fieldName: 'SubjectName', type: 'url',
            typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},
            
            {label: 'Activity Date', fieldName: 'ActivityDate', type: 'text'},
        ]);
        var action = component.get("c.loadTasks");
        var whatId = component.get("v.recordId");
        action.setParams({
            "recordId":whatId
        });
            
            
            
            
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var records =response.getReturnValue();
                records.forEach(function(record){
                   
                    record.SubjectName = '/'+record.Id;
                    record.ActivityDate= record.ActivityDate;
                });
                component.set("v.tasks", records);
            }
        });
        $A.enqueueAction(action);
    },
    createNewTask : function(component, event, helper) {
        var action = component.get("c.saveTask");
        var newTask = component.get("v.newTask");
        action.setParams({
            "task": newTask
        });
        action.setCallback(this,function(response){
            var state = response.getState();
   
            if(component.isValid() && state === "SUCCESS"){
                var items = component.get("v.tasks");
                items.push(response.getReturnValue());
                component.set("v.tasks",items);
            }
            else{
                console.log("Failed with state "+state);
            }
        });
        $A.enqueueAction(action);
    }
})

Please Mark it As best Answer if it Helps.
Thank You!
This was selected as the best answer
Jonathan Wolff 7Jonathan Wolff 7
Thank you very much for the quick help. Do you know a possibility to only show my tasks instead of all tasks? I dont want every user to see all the tasks of others
CharuDuttCharuDutt
Hii Jonathan Wolff
Copy Paste the Changes In Apec Class
string userId = UserInfo.getUserId();

SELECT Subject, ActivityDate FROM Task WHERE WhatId=:recordId And ActivityDate>= TODAY AND ActivityDate  <= Next_N_Days:3 AND OwnerId=:userId 

Please Like The Answer if it Helps.
Thank You!
Jonathan Wolff 7Jonathan Wolff 7
Could you just tell me where to add this part of your code:
string userId = UserInfo.getUserId();

I got an error when I added it in apex class
Thank you! :D
CharuDuttCharuDutt
Hii Jonathan Wolff
public with sharing class TaskController {
  @AuraEnabled
  public static List<Task> loadTasks(Id recordId){
      string userId = UserInfo.getUserId();
    return[SELECT Subject, ActivityDate FROM Task WHERE WhatId=:recordId And ActivityDate>= TODAY 
           AND ActivityDate  <= Next_N_Days:3 AND OwnerId=:userId ];
  }
  @AuraEnabled
  public static Task saveTask(Task task){
    upsert task;
    return task;
   }
}

Please Like The Answer if it Helps.
Thank You!