0

I have created LWC, which is having controller to fetch task records from apex controller. but it is again giving me same error "Object Task is not supported in UI API". can any one please let me now what is wrong in this code.

js file:

import { LightningElement, api, wire, track } from 'lwc';
import { getSObjectValue }                    from '@salesforce/apex';
import GetTaskRecords                         from '@salesforce/apex/FetchTaskLWCController.GetTaskRecords';
import task_Object                            from '@salesforce/schema/Task';
import Task_Assigned_To                       from '@salesforce/schema/Task.OwnerId';
import Task_Status                            from '@salesforce/schema/Task.Status';
import Task_Priority                          from '@salesforce/schema/Task.Priority';
import Task_Due_Date                          from '@salesforce/schema/Task.ActivityDate';
import Task_Type                              from '@salesforce/schema/Task.Type';
import Task_Related_to_Account                from '@salesforce/schema/Task.WhatId';
import Task_Subject                           from '@salesforce/schema/Task.Subject';
import Task_Comments                          from '@salesforce/schema/Task.Description';


export default class ApexWireMethodToFunction extends LightningElement {
    @api TaskRecord;
    @api recordId;
    @track tasks;
    @track error;

    taskObject = task_Object;

    @wire(GetTaskRecords, {Id: '$TaskRecord'})
    task;

    get assignedto() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Assigned_To) : '';
    }
    get status() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Status) : '';
    }
    get priority() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Priority) : '';
    }
    get duedate() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Due_Date) : '';
    }
    get type() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Type) : '';
    }
    get relatedtoaccount() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Related_to_Account) : '';
    }
    get subject() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Subject) : '';
    }
    get comments() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Comments) : '';
    }

    wiredTasks({ error, data })
    {
        if (data) 
        {
            this.tasks = data;
            this.error = undefined;
        } else if (error) 
        {
            this.error = error;
            this.tasks = undefined;
        }
    }
}

html file:

<template>
<lightning-card>
    <!-- Opportunity -->
      <!-- Display -->
        <template if:true={TaskRecord}>
            <lightning-record-form 
            record-id={TaskRecord}
            object-api-name={taskObject}
            >
            <!-- Messages -->
            <lightning-messages></lightning-messages>
            <div class="slds-p-bottom_large slds-p-left_large">
            <br/>
            <!-- Fields -->
                <lightning-accordion active-section-name="A">
                    <lightning-accordion-section name="A" label="Task Details">
                        <div class="slds-grid" style="color:rgb(15, 15, 15); font-size:13px">
                            <div class="slds-col slds-size_1-of-2">
                                <div class=" acc slds-list_horizontal slds-border_bottom">
                                    <label>Subject</label>
                                    {subject}
                                </div>
                                <div class=" acc slds-list_horizontal slds-border_bottom">
                                    <label>Status</label>
                                    {status}
                                </div>
                                <div class=" acc slds-list_horizontal slds-border_bottom">
                                    <label>Related To Account</label>
                                    {relatedtoaccount}
                                </div>
                                <div class="acc slds-border_bottom slds-list_horizontal">
                                    <label>Priority</label>
                                    {priority}
                                </div>
                                <div class="formfield slds-list_horizontal slds-border_bottom">
                                    <label>Assigned To</label>
                                    <div class="a11">{assignedto}</div>
                                </div>
                            </div>
                            <div class="slds-col slds-size_2-of-2">
                                <div class="slds-col slds-size_1-of-2">
                                    <div class="formfield slds-border_bottom slds-list_horizontal">
                                        <label>Due Date</label>
                                        {duedate}
                                    </div>
                                    <div class="formfield slds-border_bottom slds-list_horizontal">
                                        <label>Type</label>
                                        {type}
                                    </div>
                                    <div class="formfield slds-border_bottom slds-list_horizontal">
                                        <label>Comments</label>
                                        {comments}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </lightning-accordion-section>
                </lightning-accordion>
            </div>
            <br/>
        </lightning-record-form>
        </template>

</lightning-card>

js-meta.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="partnerOpportunityRecordRead">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>partner Task Record Read LWC</masterLabel>
    <targets>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
            <property name="TaskRecord" type="String" default="Record_ID"/>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

Controller :

 public with sharing class FetchTaskLWCController
{
    @AuraEnabled(cacheable=true)
    public static List<Task> GetTaskRecords() 
    {
        return [SELECT Id, OwnerId, Status, Priority, ActivityDate, Type, WhatId, Subject, Description FROM Task];
    }
}

As task and event are unsupported objects in UI API, so took apex controller approach.

4
  • In your HTML file, I don't think you are making use of lightning-record-form, you are displaying task data using custom code. So, you should remove the lightning-record-form from your HTML code as lightning-record-form doesn't support task object.
    – Avi Rai
    Commented Feb 4, 2020 at 9:04
  • hey avi.. have removed lightning-record-form, I can see field label but it is not showing any value
    – Snehal
    Commented Feb 4, 2020 at 9:23
  • 1
    You should first check whether you have the record Id in 'TaskRecord' variable and then are you getting the task record from the apex in javascript by console logging.
    – Avi Rai
    Commented Feb 4, 2020 at 10:17
  • @AviRai have checked it, also just to make sure I've directly passes id of record in js..still it didn't work
    – Snehal
    Commented Feb 4, 2020 at 16:44

2 Answers 2

1

Here, from your code, I have removed some extra lines in HTML and JS files and mainly changed the apex method to return only one task in apex controller.

In HTML file:

<template>
    <lightning-card>
        <!-- Opportunity -->
        <!-- Display -->
        <div class="slds-p-bottom_large slds-p-left_large">
            <br />
            <!-- Fields -->
            <lightning-accordion active-section-name="A">
                <lightning-accordion-section name="A" label="Task Details">
                    <div class="slds-grid" style="color:rgb(15, 15, 15); font-size:13px">
                        <div class="slds-col slds-size_1-of-2">
                            <div class=" acc slds-list_horizontal slds-border_bottom">
                                <label>Subject</label>
                                {subject}
                            </div>
                            <div class=" acc slds-list_horizontal slds-border_bottom">
                                <label>Status</label>
                                {status}
                            </div>
                            <div class=" acc slds-list_horizontal slds-border_bottom">
                                <label>Related To Account</label>
                                {relatedtoaccount}
                            </div>
                            <div class="acc slds-border_bottom slds-list_horizontal">
                                <label>Priority</label>
                                {priority}
                            </div>
                            <div class="formfield slds-list_horizontal slds-border_bottom">
                                <label>Assigned To</label>
                                <div class="a11">{assignedto}</div>
                            </div>
                        </div>
                        <div class="slds-col slds-size_2-of-2">
                            <div class="slds-col slds-size_1-of-2">
                                <div class="formfield slds-border_bottom slds-list_horizontal">
                                    <label>Due Date</label>
                                    {duedate}
                                </div>
                                <div class="formfield slds-border_bottom slds-list_horizontal">
                                    <label>Type</label>
                                    {type}
                                </div>
                                <div class="formfield slds-border_bottom slds-list_horizontal">
                                    <label>Comments</label>
                                    {comments}
                                </div>
                            </div>
                        </div>
                    </div>
                </lightning-accordion-section>
            </lightning-accordion>
        </div>
        <br />
    </lightning-card>
</template>

In JS file:

import { LightningElement, api, wire, track } from 'lwc';
import { getSObjectValue } from '@salesforce/apex';
import GetTaskRecords from '@salesforce/apex/FetchTaskLWCController.GetTaskRecords';
import task_Object from '@salesforce/schema/Task';
import Task_Assigned_To from '@salesforce/schema/Task.OwnerId';
import Task_Status from '@salesforce/schema/Task.Status';
import Task_Priority from '@salesforce/schema/Task.Priority';
import Task_Due_Date from '@salesforce/schema/Task.ActivityDate';
import Task_Type from '@salesforce/schema/Task.Type';
import Task_Related_to_Account from '@salesforce/schema/Task.WhatId';
import Task_Subject from '@salesforce/schema/Task.Subject';
import Task_Comments from '@salesforce/schema/Task.Description';


export default class ApexWireMethodToFunction extends LightningElement {
    @api TaskRecord;
    @api recordId;
    @track tasks;
    @track error;

    taskObject = task_Object;

    @wire(GetTaskRecords, { taskId: '$TaskRecord' })
    task;

    get assignedto() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Assigned_To) : '';
    }
    get status() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Status) : '';
    }
    get priority() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Priority) : '';
    }
    get duedate() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Due_Date) : '';
    }
    get type() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Type) : '';
    }
    get relatedtoaccount() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Related_to_Account) : '';
    }
    get subject() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Subject) : '';
    }
    get comments() {
        return this.task.data ? getSObjectValue(this.task.data, Task_Comments) : '';
    }
}

In Apex controller:

public with sharing class FetchTaskLWCController {
    @AuraEnabled(cacheable=true)
    public static Task GetTaskRecords(Id taskId) 
    {
        Task task = [SELECT Id, OwnerId, Status, Priority, ActivityDate, Type, WhatId, Subject, Description FROM Task WHERE Id =: taskId LIMIT 1];
        System.debug('task'+task);
        return task;
    }
}
4
  • I think I have mentioned this in my post. Commented Feb 5, 2020 at 2:47
  • Yes, right but even then she was not getting results and there must be some little mistake so I refined HTML and JS files along with the apex controller just to make sure she will get her task done now.
    – Avi Rai
    Commented Feb 5, 2020 at 5:15
  • hey @AviRai & rahulgawale, thanks guys for your help. I resolved this issue yesterday itself. there is silly mistake in html file, because of which it is not showing up any value. I did remove that tag and it is now showing expected results. Still thank you so much for help :)
    – Snehal
    Commented Feb 5, 2020 at 6:26
  • Great, anyways its always better to specify what steps you follwed or what changes you made to resolve the problem. Which will help other people as well Commented Feb 5, 2020 at 7:16
0

Your apex method is returning a list of tasks public static List<Task> GetTaskRecords(){...}. So the data in the wired method result is a list of objects, not an Object.

As you are working on a single task, you can change the method signature to return a single task like below.

You are also missing to send a task id to the apex method.

@AuraEnabled(cacheable=true)
public static Task getTaskRecord(Id taskId){
    return [SELECT Id, OwnerId, Status, Priority, ActivityDate, Type, WhatId, Subject, Description FROM Task WHERE Id = :taskId];
}

Js code is like.

import getTaskRecord from '@salesforce/apex/FetchTaskLWCController.getTaskRecord';
//..
//... 
@wire(getTaskRecord, { taskId: '$TaskRecord' })
task;

Also, it's a good idea to follow naming conventions in your code. See I have changed method name from GetTaskRecords to getTaskRecord

2
  • i have done same changes, to send task id in method. but not sure why this is still not working. it is still showing me labels, not value of fields.
    – Snehal
    Commented Feb 4, 2020 at 16:39
  • @Snehal did you try to debug the apex method and wired method? Commented Feb 4, 2020 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .