Lightning Web Components(LWC)
A modern Salesforce programming framework based on JavaScript, HTML, and Web Standards.
1) JavaScript Basics for LWC
Explanation:
LWC uses modern ES6 JavaScript.
Key Concepts:
- Variables:
let,const - Functions (normal + arrow)
- Classes & modules
- Promises & async/await
- Array functions (map, filter, reduce)
Example:
let name = 'Muni';
const multiplier = 2;
function greet() {
console.log(`Hello ${name}`);
}
const double = (num) => num * multiplier;
Why important?
All LWC components internally rely on JavaScript for logic.
2) HTML Templates
Explanation:
Every LWC has an HTML file that defines the UI layout.
Example LWC HTML:
<template>
<h1>Hello {fullName}</h1>
<lightning-button label="Click Me" onclick={handleClick}></lightning-button>
</template>
Binding Types:
{propertyName}→ One-way data bindingonclick={handler}→ Event binding
3) LWC Lifecycle Hooks
Explanation:
Hooks are special functions that run during component load, update, or removal.
Most Used Hooks:
constructor()connectedCallback()renderedCallback()disconnectedCallback()
Example:
connectedCallback() {
console.log('Component loaded');
}
renderedCallback() {
console.log('DOM rendered');
}
4) Lightning Data Service (LDS)
Explanation:
LDS lets you access Salesforce data without writing Apex.
Common Features:
- Retrieve record
- Create/Update record
- Delete record
Example Using LDS:
import { getRecord } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
account;
Purpose:
- No SOQL
- No Apex
- Secure and fast
5) Events in LWC
Explanation:
Events connect child → parent or communicate between components.
Types:
- Custom Events
- Lightning Message Service (LMS)
Example — Custom Event:
Child Component
handleClick() {
this.dispatchEvent(new CustomEvent('send', {
detail: { msg: 'Hello Parent' }
}));
}
Parent Component
<c-child onsend={handleMessage}></c-child>
handleMessage(event) {
console.log(event.detail.msg);
}
6) Calling Apex from LWC
Explanation:
When LDS can’t handle complex logic, Apex is used.
Example:
Apex class
public with sharing class AccountService {
@AuraEnabled(cacheable=true)
public static List<Account> getHotAccounts() {
return [SELECT Name, Rating FROM Account WHERE Rating='Hot'];
}
}
LWC JavaScript
import getHotAccounts from '@salesforce/apex/AccountService.getHotAccounts';
@wire(getHotAccounts)
accounts;
7) Wire Service
Explanation:
@wire is used to read Salesforce data automatically and reactively.
Example:
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
record;
Key Benefits:
- Auto-refresh
- Cache support
- Declarative
8) Navigation & UI Components
Explanation:
LWC has built-in components under the lightning namespace.
Example UI Components:
<lightning-input><lightning-card><lightning-button><lightning-datatable><lightning-record-form>
Navigation Example:
import { NavigationMixin } from 'lightning/navigation';
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
objectApiName: 'Account',
actionName: 'view'
}
});
9) Deploying LWCs
Explanation:
LWCs are deployed using:
- Change Sets
- VS Code + Salesforce CLI
- DevOps Center
- Git-based CICD pipelines
Typical Deployment Using VS Code:
sfdx force:source:deploy -p force-app/main/default/lwc
Package structure:
myComponent/
myComponent.html
myComponent.js
myComponent.js-meta.xml