Notes
HTML
<template>
<lightning-card title="Account Selection">
<lightning-combobox
label="Select an Account"
value={selectedAccountId}
placeholder="Choose an Account"
options={accounts}
onchange={handleAccountChange}>
</lightning-combobox>
</lightning-card>
<template if:true={selectedAccountId}>
<lightning-card title="Related Cases">
<lightning-tabset>
<template for:each={cases} for:item="caseRecord">
<lightning-tab key={caseRecord.Id} label={caseRecord.CaseNumber}>
<p><strong>Subject:</strong> {caseRecord.Subject}</p>
<p><strong>Status:</strong> {caseRecord.Status}</p>
</lightning-tab>
</template>
</lightning-tabset>
</lightning-card>
</template>
</template>
JavaScript
import { LightningElement, wire, track } from 'lwc';
import getAccounts from '@salesforce/apex/AccountCasesController.getAccounts';
import getCasesByAccountId from '@salesforce/apex/AccountCasesController.getCasesByAccountId';
export default class AccountCasesTabs extends LightningElement {
@track accounts = [];
selectedAccountId;
cases = [];
@wire(getAccounts)
wiredAccounts({ error, data }) {
if (data) {
this.accounts = data.map(acc => ({ label: acc.Name, value: acc.Id }));
} else if (error) {
console.error('Error fetching accounts:', error);
}
}
handleAccountChange(event) {
this.selectedAccountId = event.detail.value;
this.fetchCases();
}
fetchCases() {
if (!this.selectedAccountId) return;
getCasesByAccountId({ accountId: this.selectedAccountId })
.then(data => {
this.cases = data || [];
})
.catch(error => {
console.error('Error fetching cases:', error);
});
}
}
Meta XML
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="FormattedTimeLWC">
<apiVersion>59.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
</targets>
</LightningComponentBundle>
Apex
public with sharing class AccountCasesController {
@AuraEnabled(cacheable=true)
public static List<Account> getAccounts() {
return [SELECT Id, Name FROM Account];
}
@AuraEnabled(cacheable=true)
public static List<Case> getCasesByAccountId(String accountId) {
return [SELECT Id, CaseNumber, Status, Subject FROM Case WHERE AccountId = :accountId];
}
}