1

How can I use my wrapperclass with a lightning:datatable, Is that possible? At the moment I use

<apex:pageBlockTable value="{!wrapperList}" var="wl" title="all activities">

to display the result.

CLASS

    public class DokuBox_6 {
    
    public List<Wrapper> wrapperList {get; set;}
    public String currentRecordId {get; set;}
    
    public DokuBox_6() {
        currentRecordId = ApexPages.currentPage().getParameters().get('id');
        
        if(wrapperList == null) {
            wrapperList = new List<Wrapper>();
            
            for(Task a: [select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status from Task where WhoId =: currentRecordId AND Subject LIKE 'E-Mail:%']) {
                wrapperList.add(new Wrapper(a, 'E-Mail'));
            }
            for(Task a: [select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status from Task where WhoId =: currentRecordId AND (NOT Subject LIKE 'E-Mail:%')]) {
                wrapperList.add(new Wrapper(a, 'Aufgabe'));
            }            
            for(Event b: [select Id, Subject, CreatedDate, StartDateTime, Createdby.Name, Owner.Name from Event where WhoId =: currentRecordId]) {
                wrapperList.add(new Wrapper(b, 'Termin'));
            }          
            for(ContentDocumentLink d: [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.createdDate, ContentDocument.Createdby.Name, ContentDocument.FileExtension FROM ContentDocumentLink WHERE LinkedEntityId =: currentRecordId AND ContentDocument.FileExtension = 'snote']) {
                wrapperList.add(new Wrapper(d, 'Notiz'));
            }
            for(ContentDocumentLink d: [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.createdDate, ContentDocument.Createdby.Name, ContentDocument.FileExtension FROM ContentDocumentLink WHERE LinkedEntityId =: currentRecordId AND ContentDocument.FileExtension <> 'snote']) {
                wrapperList.add(new Wrapper(d, 'Datei'));
            }            
            wrapperList.sort();
        }
    }
    
    public class Wrapper implements Comparable {
        
        private final String objectLabel;
        private final Id id;
        private final String subject;
        private final DateTime createDate;
        private final String Ersteller;
        private final String Typ;
        private final String Status;
        
        public wrapper(Task t, String oName) { 
            objectLabel     = oName;
            id              = t.Id;
            subject         = t.Subject;
            createDate      = t.CreatedDate;
            Ersteller       = t.Owner.Name;
            Typ             = t.RecordType.Name;
            Status          = t.Status;
        }
        
        public wrapper(Event e, String oName) {
            objectLabel     = oName;
            id              = e.Id;
            subject         = e.Subject;
            createDate      = e.StartDateTime;
            Ersteller       = e.Owner.Name;
        }
        
        public wrapper(ContentDocumentLink f, String oName) {
            objectLabel     = oName;
            id              = f.ContentDocumentId;
            subject         = f.ContentDocument.Title;
            createDate      = f.ContentDocument.CreatedDate;
            Ersteller       = f.ContentDocument.CreatedBy.Name;
            Typ             = f.ContentDocument.FileExtension;
        }
        
        public Id getId() {
            return id;
        }
        
        public String getSubject() {
            return subject;
        }
        
        public String getTyp() {
            return Typ;
        }    
        
        public DateTime getCreatedDate() {
            return createDate;
        }
        
        public String getObjectLabel() {
            return objectLabel;
        }      

        public String getErsteller() {
            return Ersteller;
        }         

        public String getStatus() {
            return Status;
        }         
        
        // Sort in ascending order
        public Integer compareTo(Object compareTo) {
            Wrapper compareToActivity = (Wrapper) compareTo;
            DateTime compareToCreateDate = compareToActivity.getCreatedDate();
            if (createDate > compareToCreateDate) {
                return -1;
            }
            if (createDate < compareToCreateDate) {
                return 1;
            }
            return 0;
        }
    }
    
}

1 Answer 1

0

Yes, you can. There are 2 ways you can go about it.

One option is to mark your wrapper class properties as @AuraEnabled and make them public.

public class Wrapper {
    @AuraEnabled
    public String objectLabel;

    @AuraEnabled
    public String id;

    @AuraEnabled
    public String subject;

    @AuraEnabled
    public DateTime createDate;
} 

You can then directly bind the list from Apex to the data attribute of lightning-datatable.

import { LightningElement, wire, api } from 'lwc';
import getWrapperData from '@salesforce/apex/DokuBox_6.getWrapperData';

const columns = [
    { label: 'Label', fieldName: 'objectLabel' },
    { label: 'Subject', fieldName: 'subject', type: 'string' },
    { label: 'Created Date', fieldName: 'createDate', type: 'date' },
];

export default class Datatabledemo extends LightningElement {
    @api recordId;

    data = [];
    columns = columns;

    @wire(getWrapperData, {currentRecordId: '$recordId'})
    dataFn({data}){
        this.data = data;
    }
}
<template>
    <lightning-datatable
            key-field="id"
            data={data}
            columns={columns}>
    </lightning-datatable>
</template>

If you dont want to make your wrapper class properties public, you can serialize the list and send it to your component.

@AuraEnabled(cacheable=true)
public static String getWrapperData(String currentRecordId) {
    List<Wrapper> wrapperList = new List<Wrapper>();
    for(Task a: [select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status from Task where WhoId =: currentRecordId AND Subject LIKE 'E-Mail:%']) {
        wrapperList.add(new Wrapper(a, 'E-Mail'));
    }
    for(Task a: [select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status from Task where WhoId =: currentRecordId AND (NOT Subject LIKE 'E-Mail:%')]) {
        wrapperList.add(new Wrapper(a, 'Aufgabe'));
    }            
    for(Event b: [select Id, Subject, CreatedDate, StartDateTime, Createdby.Name, Owner.Name from Event where WhoId =: currentRecordId]) {
        wrapperList.add(new Wrapper(b, 'Termin'));
    }          
    for(ContentDocumentLink d: [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.createdDate, ContentDocument.Createdby.Name, ContentDocument.FileExtension FROM ContentDocumentLink WHERE LinkedEntityId =: currentRecordId AND ContentDocument.FileExtension = 'snote']) {
        wrapperList.add(new Wrapper(d, 'Notiz'));
    }
    for(ContentDocumentLink d: [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.createdDate, ContentDocument.Createdby.Name, ContentDocument.FileExtension FROM ContentDocumentLink WHERE LinkedEntityId =: currentRecordId AND ContentDocument.FileExtension <> 'snote']) {
        wrapperList.add(new Wrapper(d, 'Datei'));
    }            
    wrapperList.sort();          
            
    return JSON.serialize(wrapperList);
}

Inside your component, you just need to convert the String back to JSON, using JSON.parse.

@wire(getWrapperData)
dataFn({data}){
    if(data){
        this.data = JSON.parse(data);
    }
}
5
  • Ok, than I have to create a lightning component and component controller or should I add the code to the visualfoce page? At the moment I display the datatable on a visualforce page. Sorry, but I'm not realy familiar with lightning components.
    – peXeq
    Aug 3, 2021 at 11:36
  • Ideally, you should create a component to keep things modular, but you can directly embed a datatable too. (Theoretically... I havent tried that though. As long as you are able to pass the data that the datatable needs, you should be good.) Aug 3, 2021 at 11:58
  • component would be great, but I'm not familiar with this kind of code. Could you give some code for the component and the component controller?
    – peXeq
    Aug 3, 2021 at 12:20
  • I just updated the answer with those snippets - the second and third code snippets are the component's controller and component. The third snippet is a new Apex method that you have to add to your class. Finally, replace the dataFn from the second code snippet with the 4th one (basically just adding JSON.parse) Aug 3, 2021 at 12:59
  • thanks, but I don't understand the whole construction. If my apex with the code from the 4th snippet I have no more items to return. Sorry but this is new for me.
    – peXeq
    Aug 4, 2021 at 11:27

You must log in to answer this question.

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