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
Shravan Kumar 71Shravan Kumar 71 

Display data in Column using LWC

I am trying to create a view to show activity information. However the below code is not displaying any information, only header is showing. Please help.
HTML Component :

<template>
    <lightning-card class="slds-text-title_bold"  title = "Activity Information">
        <div class="slds-p-around_medium lgc-bg" style="height: 300px;">
            <lightning-datatable
                    key-field="id"
                    data={data}
                    columns={columns}>
            </lightning-datatable>
        </div>
    </lightning-card>
</template>
 
JS File:

import { LightningElement,api,wire,track} from 'lwc';
import ActivitySearchController from '@salesforce/apex/ActivityLeadPage.ActivitySearchController'

const columns = [
    { label: 'Subject', fieldName: 'Subject' },
    { label: 'Due Date', fieldName: 'ActivityDate' },
    { label: 'Status', fieldName: 'Status' },
];

export default class ActivityLeadPageComponent extends LightningElement {
    @api recordId;
    @track data = [];
    @track columns = columns;
    @wire(ActivitySearchController, { currentID: '$recordID'})
    TaskList;
}
 
Controller :

public class ActivityLeadPage{
    
    @AuraEnabled(cacheable=true)
    public static List<Task> ActivitySearchController(String currentID){
        List<Task> TaskList = new List<Task>();
        Map<Id,Lead> leadMap = new Map<Id,Lead>();
        if(currentID.startsWith('00Q')){
            try{
                List <Lead> leadList = [SELECT id, Email FROM Lead WHERE ID=:currentId];
                String ldEmail       = leadList[0].Email;
                Set<String> emailIds = new Set<string>();
                if(ldEmail!=null){
                    emailIds.add(ldEmail);
                }               
                
                TaskList = getTaskList(emailIds);
                
            }           
            catch(Exception e){
                system.debug('getlinenumber-->'+ e.getMessage() +' line '+ e.getLineNumber());
            } 
        }
        return TaskList;
    }
    
    public static List<Task> getTaskList (Set<String> emailIds) {  
        Map<Id,Lead> leadMap = new Map<Id,Lead>();      
        leadMap = new Map<Id,Lead>([SELECT id, Email FROM Lead Where Email IN:emailIds]);       
        
        Set<Id> leadID = new Set<Id>(); 
        for(Lead lE : leadMap.values()){
            leadID.add(lE.id);            
        }  
        
        List<Task> TaskList = [Select id, Subject, Description, who.Type, What.Type, Priority, Status, ActivityDate,CreatedDate, LastModifiedDate FROM Task 
                               WHERE whoId IN:leadID ORDER BY createddate DESC LIMIT 20];
        
        if(TaskList.size() == 0){
            Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
        }
        
        return TaskList;
    }   
}
Best Answer chosen by Shravan Kumar 71
Naveen KNNaveen KN
Hi Shravan, Once you get the data from the apex, you have to map the data to the client-side variable. 

refer this link https://www.codengine.in/2019/08/salesforce-lightning-datatable-example-with-lwc-lightning-web-component.html 

it would be like 
@wire(GetContactList, {})
    ApexResponse({ errordata }) {
        if (data) {
            this.data = data;
        } else if (error) {
            //test
        }
    }

Try this and let me know if any issues

--
Naveen K N

All Answers

Alain CabonAlain Cabon

Access to the record ID.: '$recordId'

https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.use_record_context

Javascript is case sensitive but not Apex.
Shravan Kumar 71Shravan Kumar 71
Hello Alain,

Many thanks for correction, however the table is still showing without values. The record has 3 tasks. 

User-added image
Shravan Kumar 71Shravan Kumar 71
Still no luck, Alain. I changed my apex class to single method but result still the same. 
public class ActivityLeadPage{
    
    @AuraEnabled(cacheable=true)
    public static List<Task> ActivitySearchController(String currentID){
        List<Task> TaskList = new List<Task>();
        Map<Id,Lead> leadMap = new Map<Id,Lead>();
        if(currentID.startsWith('00Q')){
            try{
                List <Lead> leadList = [SELECT id, Email FROM Lead WHERE ID=:currentId];
                String ldEmail       = leadList[0].Email;
                Set<String> emailIds = new Set<string>();
                if(ldEmail!=null){
                    emailIds.add(ldEmail);
                }               
                
                leadMap = new Map<Id,Lead>([SELECT id, Email FROM Lead Where Email IN:emailIds]);       
                
                Set<Id> leadID = new Set<Id>(); 
                for(Lead lE : leadMap.values()){
                    leadID.add(lE.id);            
                }  
                
                TaskList = [Select id, Subject, Description, who.Type, What.Type, Priority, Status, ActivityDate,CreatedDate, LastModifiedDate FROM Task 
                                       WHERE whoId IN:leadID ORDER BY createddate DESC LIMIT 20];
                           
                if(TaskList.size() == 0){
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
                }
                
            }           
            catch(Exception e){
                system.debug('getlinenumber-->'+ e.getMessage() +' line '+ e.getLineNumber());
            } 
        }
        return TaskList;
    }   
}
 
import { LightningElement,api,wire,track} from 'lwc';
import ActivitySearchController from '@salesforce/apex/ActivityLeadPage.ActivitySearchController'

const columns = [
    { label: 'Subject', fieldName: 'Subject' },
    { label: 'Due Date', fieldName: 'ActivityDate' },
    { label: 'Status', fieldName: 'Status' },
];

    export default class ActivityLeadPageComponent extends LightningElement {
        @api recordId;
        @track data = [];
        @track columns = columns;
        @wire(ActivitySearchController, { currentID: '$recordId'})
        TaskList;
    }

 
Naveen KNNaveen KN
Hi Shravan, Once you get the data from the apex, you have to map the data to the client-side variable. 

refer this link https://www.codengine.in/2019/08/salesforce-lightning-datatable-example-with-lwc-lightning-web-component.html 

it would be like 
@wire(GetContactList, {})
    ApexResponse({ errordata }) {
        if (data) {
            this.data = data;
        } else if (error) {
            //test
        }
    }

Try this and let me know if any issues

--
Naveen K N
This was selected as the best answer
Shravan Kumar 71Shravan Kumar 71
Hello Naveen,

That's worked. Thank you so much.

I would like to add one more column to the table i.e., Object. This column will have value based on the whoID and WhatID. For eg: if task whoID beings with 00Q then the value should be Lead, similarly if the task whoID beings with 001 then the value will be Account and if whatID being with 006 then value will be Opportunity. Appreciate your guidance.
Naveen KNNaveen KN
updating the latest link here for data for lwc data table implementation  

https://www.codekiat.com/2019/08/salesforce-lightning-datatable-example-with-lwc-lightning-web-component.html