Sub topic 13
Global actions
This chapter shows how to expose a Lightning web component as a global action. It assumes you have built a record screen action, as covered in earlier chapters. You should know js-meta.xml targets, lightning-quick-action-panel, and CloseActionScreenEvent.
What a global action is
A global action is a quick action that is not tied to one record. Users start it from the global actions menu (the + icon in the Lightning Experience header) or from the mobile action bar. Typical uses are creating a record, logging a call, or opening a small form from any page.
The main difference from a record action is the missing record context. The platform does not pass recordId or objectApiName to a global action. Your component must collect or look up everything it needs.
| Aspect | Record action | Global action |
|---|---|---|
| Target | `lightning__RecordAction` | `lightning__GlobalAction` |
| Record context | `recordId`, `objectApiName` provided | Not provided |
| Where it appears | Page layout of one object | Global publisher layout |
| Typical job | Change the current record | Create something or start a task |
Expose the component
Change the target in js-meta.xml. A global action does not use the record-action actionType setting, so a minimal bundle is enough.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__GlobalAction</target>
</targets>
</LightningComponentBundle>Build the component
This example creates an Account from a single name field. The template reuses the panel layout from the screen action chapter.
<template>
<lightning-quick-action-panel header="Quick Account">
<lightning-input
label="Account name"
value={accountName}
required
onchange={handleChange}>
</lightning-input>
<div slot="footer">
<lightning-button variant="neutral" label="Cancel" onclick={handleCancel}></lightning-button>
<lightning-button variant="brand" label="Create" onclick={handleCreate} disabled={isSaving}></lightning-button>
</div>
</lightning-quick-action-panel>
</template>Because there is no existing record to update, the JavaScript calls createRecord instead of updateRecord.
import { LightningElement } from 'lwc';
import { CloseActionScreenEvent } from 'lightning/actions';
import { createRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class QuickAccount extends LightningElement {
accountName = '';
isSaving = false;
handleChange(event) {
this.accountName = event.target.value;
}
handleCancel() {
this.dispatchEvent(new CloseActionScreenEvent());
}
async handleCreate() {
if (!this.accountName) {
return;
}
this.isSaving = true;
const fields = {};
fields[NAME_FIELD.fieldApiName] = this.accountName;
try {
await createRecord({ apiName: ACCOUNT_OBJECT.objectApiName, fields });
this.dispatchEvent(new ShowToastEvent({
title: 'Success',
message: 'Account created',
variant: 'success'
}));
this.dispatchEvent(new CloseActionScreenEvent());
} catch (error) {
this.dispatchEvent(new ShowToastEvent({
title: 'Error creating account',
message: error?.body?.message || 'Unknown error',
variant: 'error'
}));
} finally {
this.isSaving = false;
}
}
}Nothing here reads recordId. The component works the same wherever the user opens it.
Create the action and publish it
Global actions are defined and placed separately from record actions.
- In Setup, search for Global Actions and click New Action.
- Set Action Type to Lightning Web Component and choose your component.
- Enter a label, such as "Quick Account", and save.
- In Setup, open Publisher Layouts and edit the Global Layout.
- Drag the action into Salesforce Mobile and Lightning Experience Actions, then save.
Common pitfalls
- Reading
recordId. It staysundefined, so queries or updates built on it fail. If you need a record, add a lookup field such aslightning-record-pickerand let the user choose. - Missing from the menu. The action exists in Setup but was never added to the Global Layout. Users also need access to the component's objects and fields.
- Wrong target. A bundle that only declares
lightning__RecordActiondoes not appear in the Global Actions list. Deploy thelightning__GlobalActiontarget. - No refresh. A global action does not know which page is open. If the user may be viewing the changed data, tell them in the toast.
Exercise
A user opens your global action from an Account record page, and the component reads this.recordId to update that Account. What happens, and how can you fix it?
Show answer
recordId is undefined because global actions get no record context, so the update fails. Either convert the component to a record action using lightning__RecordAction, or keep it global and have the user pick the Account, for example with lightning-record-picker.