Sub topic 1
What a Quick Action Is and When to Use an LWC
Prerequisites
This chapter assumes you already know the following:
- The basic structure of a Lightning web component: an HTML template, a JavaScript class, and a
js-meta.xmlfile. - Decorators such as
@api, and how to handle events in LWC. - Salesforce record pages and page layouts.
- A basic Salesforce DX project and deployment workflow.
You don't need to have built a quick action before.
What a quick action is
A quick action is a button on a Salesforce record page, or a global menu, that starts a task for the user. Standard examples are "New Contact" or "Log a Call". Clicking the button usually opens a small modal panel. It can also run code immediately with no panel.
Declarative quick actions cover simple cases. You can create a record, update a record, or log a call by choosing fields in Setup. They break down when you need custom logic, such as conditional fields, a multi-step form, or a call to external data. A Lightning web component (LWC) fills that gap. You write the UI and the logic yourself, and Salesforce runs the component when the user clicks the action.
When an LWC is a good fit
Use an LWC when the declarative options can't express what you need. Typical reasons:
- The form needs custom validation or fields that change based on other fields.
- The action needs to call Apex or another data source before it saves.
- The action should run logic with no user input at all, such as calling a service and showing a result.
- You want a UI that matches your own design, not a generated form.
Stay with a declarative action when a field-based form is enough. It needs no code, no tests, and no deployment.
| Requirement | Better choice |
|---|---|
| Create a record with a fixed set of fields | Declarative action |
| Update a few fields with default values | Declarative action |
| Show a custom form with conditional logic | LWC screen action |
| Run logic on click with no form | LWC headless action |
The two kinds of LWC action
An LWC quick action comes in two kinds. Later chapters cover both in detail.
- A screen action opens a modal panel that shows your component. The user fills in fields and clicks a button such as Save.
- A headless action has no visible UI. The platform calls a public
invoke()method on your component when the user clicks the action.
The kind is set in the component's js-meta.xml file, using the actionType value. The difference matters because it decides how the component starts and how it ends.
What the component receives
When Salesforce runs a record quick action, it supplies the context of the record the user is viewing. Your component declares a public recordId property with @api, and the platform fills it in. With that ID you can read or update the record without asking the user which one they mean.
import { LightningElement, api } from 'lwc';
export default class ShowRecordId extends LightningElement {
@api recordId;
}Because the platform sets the value, don't assign it yourself. It may also not be available at the moment the constructor runs, so read it in handlers or getters, not in the constructor.
What you will build
Over the following chapters you will build a screen action that updates an Account description. It saves with updateRecord, shows a toast, and closes its panel. You will then build a headless action that shows a toast through invoke(). Finally you will create the action in Setup, add it to a page layout, and look at global actions, view refresh, and Jest testing.
Exercise
A team wants an action on the Opportunity page that runs a validation check against an external service the moment the user clicks it, and then shows a result message. No input is needed from the user. Should they use a declarative action, an LWC screen action, or an LWC headless action? Explain why.
Show answer
An LWC headless action. The work needs custom logic, so a declarative action doesn't fit. No form or input is needed, so a screen action's panel adds nothing. A headless action runs invoke() on click and can show the result with a toast.