TransWikia.com

task fields are not displaying value in LWC, which is used in partner community

Salesforce Asked by Snehal on January 2, 2022

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.

2 Answers

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;
    }
}

Answered by Avi Rai on January 2, 2022

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

Answered by Rahul Gawale on January 2, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP