Skip to content

Instantly share code, notes, and snippets.

@pgajek2
Created April 28, 2024 14:15
Show Gist options
  • Select an option

  • Save pgajek2/1e93f18450586cee69831330133c33c3 to your computer and use it in GitHub Desktop.

Select an option

Save pgajek2/1e93f18450586cee69831330133c33c3 to your computer and use it in GitHub Desktop.
Dynamic Function in LWC
import { LightningElement } from 'lwc';
export default class DynamicFunctionInvocation extends LightningElement {
handleDynamicInvocation(recordId) {
const prefixToHandler = {
'001': this.handleAccounts,
'003': this.handleContacts,
'006': this.handleOpportunities,
// ...
};
const recordPrefix = recordId.substring(0, 3);
if (!prefixToHandler[recordPrefix]) {
throw new Error('Record Type not supported');
}
prefixToHandler[recordPrefix](recordId);
}
handleAccounts = (recordId) => {
// ...
}
handleContacts = (recordId) => {
// ...
}
handleOpportunities = (recordId) => {
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment