I have a LWC that passes email and customerId (a custom field) to an Apex class that retrieves customer information from an external system. The LWC is working fine on a Lead Record Page.
We have the same customer Id field on Contact record as well. So, I would like to make this component flexible so that it can be used on Lead as well as Contact record page.
Is there a way to get this work? I would like to use the same component on Lead Record Page AND Contact Record Page.
Here is the JS code from the LWC:
import { LightningElement, wire, api } from 'lwc';
import {getRecord} from 'lightning/uiRecordApi';
import LEAD_EMAIL from '@salesforce/schema/Lead.Email';
import LEAD_CUSTID from '@salesforce/schema/Lead.CustId__c';
import profileInfo from '@salesforce/apex/GetProfileData.getCustInfo';
export default class ProfileData extends LightningElement {
@api recordId;
result = {};
profileData = {};
@wire(getRecord, { recordId: '$recordId', fields: [LEAD_EMAIL, LEAD_CUSTID]})
wiredRecord({data, error}) {
if(data) {
const { fields } = data
Object.keys(fields).forEach(item => {
let value = fields[item] && fields[item].value
this.result = { ...this.result, [item]: value}
})
} else if (error) {
console.error(error)
}
if(!(this.result.CustId__c == null && this.result.Email == null)) {
profileInfo({custId: this.result.CustId_c, email: this.result.Email})
.then( result => {
this.profileData = result;
})
.catch (error => {
this.error = error;
});
}
}
}