SF newbie here. I have a lightning-combobox component with a bunch of leads. When a user clicks on a particular lead, I want data related to that lead (Name, Company, Email) to be displayed. I'm hoping to achieve this by filtering the avaialble options by the Lead ID which is passed into my onchange handler function but this doesn't seem to work as I had hoped.
I'm expecting the following to be populated but it's just empty.
<p if:true={value}>Name: {selectedLead.Name}</p>
<p if:true={value}>Company: {selectedLead.Company}</p>
<p if:true={value}>Email: {selectedLead.Email}</p>
I tested my filter function in the browser's dev console and it works fine... Can someone give some pointers as to what I'm doing wrong?
Code:
LWC
import { LightningElement, wire } from 'lwc';
import getLeads from '@salesforce/apex/LeadController.getLeads';
export default class LeadDropdownComponent extends LightningElement {
// tried adding the @track decorator for the following variables but issue still remains
leadOptions;
selectedLead;
value;
@wire(getLeads)
leads({ error, data }) {
if (data) {
console.log(data)
try {
this.leadOptions = data.map(d => {
return { label: d.Name, value: d.Id };
});
} catch (error) {
console.error(error);
}
} else if (error) {
console.error(error);
}
}
handleChange(event) {
this.value = event.detail.value;
this.selectedLead = this.leadOptions.filter(lead => lead.Id === event.detail.value)[0];
}
}
HTML
<template>
<lightning-card title="Lead" icon-name="standard:people">
<lightning-combobox
name="lead"
label="Lead"
value={value}
options={leadOptions}
placeholder="Select Lead"
onchange={handleChange}
></lightning-combobox>
<p if:true={selectedLead}>Name: {selectedLead.Name}</p>
<p if:true={selectedLead}>Company: {selectedLead.Company}</p>
<p if:true={selectedLead}>Email: {selectedLead.Email}</p>
</lightning-card>
</template>
Controller
public with sharing class LeadController {
@AuraEnabled(cacheable=true)
public static List<Lead> getLeads() {
try {
return [SELECT Id, Name, Company, Email FROM Lead WITH SECURITY_ENFORCED];
} catch (QueryException error) {
System.debug(error);
return null;
}
}
}