In this blog post, find a sample code to display multiple objects' data in a single lightning-datatable base component in the Salesforce lightning CRM.
In this example, Subject and Activity Date fields of Task and Event objects are
wrapped into a single list in the Apex and passed to the lightning web
component.
Create a new Apex class
TaskAndEventController.apxc
for(Task t: taskList){
for(Event e: eventList){
system.debug('taskAndEventRecords >> ' + taskAndEventRecords);
public class TaskAndEventWrapper{
public string Subject{get; set;}
public Date ActivityDate{get; set;}
Create a new lightning web component
taskAndEvents.html
<lightning-card title="Related Activities ">
<lightning-datatable data={activities} columns={columns} key-field="Id">
</lightning-datatable>
</lightning-card>
</template>
taskAndEvents.js
'@salesforce/apex/TaskAndEventController.getTaskandEvents';
@wire(getActivities)
}
}
taskAndEvents.js-meta.xml
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>55.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>
Ensure that the variable name in the Apex wrapper class matches with the columns variable fieldName attribute value of the LWC JavaScript file.
Save or deploy the lightning
web component to the sandbox, add the new component to the Flexi page and test
the functionality.
It should display the
records from task and event objects as shown below
If you find any issues in implementing this, debug your lightning web component and add your queries or feedback in the comments section below.
Until next time...
0 Comments