Skip to main content

Latest Post

Custom Metadata Types vs Custom Settings vs Custom Labels

  Custom Metadata Types vs Custom Settings vs Custom Labels 💬 In plain words:  Three lookalikes: Custom Metadata = configuration that DEPLOYS with your code (best for app settings). Custom Settings = org/user-specific values, changeable at runtime (hierarchy type is great for bypass switches). Custom Labels = translatable text for the UI. 📌 Example:  API endpoint URLs per environment → Custom Metadata (deploys with code, sandbox vs prod values). A 'Bypass_Automation__c' checkbox an admin flips during data load → hierarchy Custom Setting. The word 'Submit' translated to Hindi → Custom Label. 🎬 Real-Life Example: The Fee Table Trapped Inside Code  Skyline charges a different delivery fee per city. Apex needs those rates on every booking. The Old/Bad Way:  if (city == 'Delhi') fee = 50. Else if (city == 'Mumbai') fee = 65. … Every rate change is a code change, a test run, and a deployment. Why this is bad:  Business data is trappe...

LWC Component Communication in Salesforce

 LWC (Lightning Web Components) is a framework provided by Salesforce for building web components on the Salesforce Lightning platform. LWC events are used to facilitate communication between different components in an application. Here are some examples of LWC events with code snippets:


1. Parent-Child Component Communication:


   ParentComponent.html:


   <template>

     <lightning-button label="Fire Event" onclick={fireEvent}></lightning-button>

     <c-child-component oncustomevent={handleEvent}></c-child-component>

   </template>


   ParentComponent.js:


   import { LightningElement } from 'lwc';


   export default class ParentComponent extends LightningElement {

     fireEvent() {

       const event = new CustomEvent('customevent', { detail: 'Event Data' });

       this.dispatchEvent(event);

     }


     handleEvent(event) {

       // Handle event data received from child component

       console.log(event.detail);

     }

   }

  

   ChildComponent.html:


   <template>

     <p>Child Component</p>

   </template>


   ChildComponent.js:


   import { LightningElement, api } from 'lwc';


   export default class ChildComponent extends LightningElement {

     @api

     oncustomevent(event) {

       // Handle event received from parent component

       console.log(event.detail);

     }

   }


2. Component Event Bubbling:

   ChildComponent.html:


   <template>

     <button onclick={fireEvent}>Fire Event</button>

   </template>


   ChildComponent.js:


   import { LightningElement } from 'lwc';


   export default class ChildComponent extends LightningElement {

     fireEvent() {

       const event = new CustomEvent('customevent', {

         bubbles: true, // Enable event bubbling

         composed: true, // Allow event to cross the shadow boundary

         detail: 'Event Data',

       });

       this.dispatchEvent(event);

     }

   }


   ParentComponent.html:


   <template>

     <p>Parent Component</p>

     <c-child-component oncustomevent={handleEvent}></c-child-component>

   </template>


   ParentComponent.js:


   import { LightningElement } from 'lwc';


   export default class ParentComponent extends LightningElement {

     handleEvent(event) {

       // Handle event data received from child component

       console.log(event.detail);

     }

   }


3. Component Event Propagation (Stop Propagation):


   GrandParentComponent.html:


   <template>

     <p>GrandParent Component</p>

     <c-parent-component></c-parent-component>

   </template>


   GrandParentComponent.js:


   import { LightningElement } from 'lwc';


   export default class GrandParentComponent extends LightningElement {}


   ParentComponent.html:


   <template>

     <p>Parent Component</p>

     <c-child-component oncustomevent={handleEvent}></c-child-component>

   </template>


   ParentComponent.js:


   import { LightningElement } from 'lwc';


   export default class ParentComponent extends LightningElement {

     handleEvent(event) {

       event.stopPropagation(); // Stop event propagation

     }

   }


   ChildComponent.html:


   <template>

     <button onclick={fireEvent}>Fire Event</button>

   </template>

   

   ChildComponent.js:


   import { LightningElement } from 'lwc';


   export


 default class ChildComponent extends LightningElement {

     fireEvent() {

       const event = new CustomEvent('customevent', { detail: 'Event Data' });

       this.dispatchEvent(event);

     }

   }

These examples demonstrate different scenarios of LWC event handling, including parent-child component communication, event bubbling, and event propagation. Feel free to modify and adapt these code snippets to suit your specific requirements and component structure.

Popular Posts

Salesforce LWC Code for Multi-Select Lookup

Introduction: In Salesforce Lightning Web Components (LWC), implementing a multi-select lookup field can enhance the user experience and provide greater flexibility for selecting multiple related records. In this blog post, we will walk through the process of creating a multi-select lookup field using LWC. We will cover the required code snippets and provide step-by-step instructions to help you implement this functionality in your Salesforce org.