Developing Apps using Message Channel API
Subscribing Fired Events
import { LightningElement, wire } from "lwc";
import {
subscribe,
unsubscribe,
APPLICATION_SCOPE,
MessageContext
} from "lightning/messageService";
// Message Channel to catch events of focused cell in the sheet
import MC_FOCUS_CELL from "@salesforce/messageChannel/msmxSheet__focusCell__c";
// A component that subscribes the events
export default class MyComponent extends LightningElement {
@wire(MessageContext)
messageContext;
subscr = null;
connectedCallback() {
// Create subscription to Message Channel
// For details, please refer to the developer documents of Salesforce Lightning Web Component
this.subscr = subscribe(
this.messageContext,
MC_FOCUS_CELL,
(msg) => this.handleFocusCell(msg),
{ scope: APPLICATION_SCOPE }
);
}
disconnectedCallback() {
// Unsubscribe the subscription to Message Channel
if (this.subscr) {
unsubscribe(this.subscr);
}
this.subscr = null;
}
handleFocusCell(message) {
// do something important
console.log(message.cell.recordId);
}
}
Event Types and Corresponding Message Channels
Select Records
Load Complete
Focus Cell
Fire Custom Event
Setting Parameters
Message Channel for Setting Parameters
Execute Commands
Message Channel for Command Execution
Types of Executable Commands
Focus Sheet
Save Records
Last updated