Respond to Location Filter Changes

Custom components should respond to global filter changes if possible, to provide a cohesive Command Center user experience.

Components that must respond to global filter changes should subscribe to the Command Center standard Lightning Message Service channels. To reference a message channel, import it from the @salesforce/messageChannel scoped module. To use the Lightning Message Service APIs:

  • Import the following functions from lightning/messageService: subscribe, unsubscribe, MessageContext, and APPLICATION_SCOPE.
  • Import COMMAND_CENTER_MSG_CHANNEL from @salesforce/messageChannel/lightning__CommandCenterMessageChannel.

The following example, from the sample app at https://github.com/forcedotcom/WorkDotCom-Partners, demonstrates subscribing and unsubscribing to the Command Center message channel and listening for events fired.

1import { LightningElement, wire, track } from 'lwc';
2import { subscribe, MessageContext, unsubscribe, APPLICATION_SCOPE } from 'lightning/messageService';
3
4/*** Message Channel ***/
5import COMMAND_CENTER_MSG_CHANNEL from '@salesforce/messageChannel/lightning__CommandCenterMessageChannel';
6
7export default class lwc_component extends LightningElement{
8    @wire(MessageContext)
9    messageContext;
10    
11    @track globalLocationName;
12    @track globalLocationId;
13    
14    subscription;
15
16    connectedCallback() {
17        this.subscribeToChannel();
18    }
19
20    /**
21     * Subscribe to Command Center Message Channel to listen to global filter changes
22     */
23    subscribeToChannel() {
24        if (!this.subscription) {
25            this.subscription = subscribe(this.messageContext, COMMAND_CENTER_MSG_CHANNEL, message => this.handleEvent(message), {
26                scope: APPLICATION_SCOPE
27            });
28        }
29    }
30
31    /**
32     * Any time global filter changes are captured get updated values
33     * @param  {} message
34     */
35    handleEvent(message) {
36        switch (message.EventType) {
37            case 'CC_LOCATION_CHANGE': {
38                /* This event returns two attributes within it's EventPayload (locationName & locationId) */
39                this.globalLocationName = message.EventPayload.locationName;
40                this.globalLocationId = message.EventPayload.locationId;
41          
42                break;
43            }
44
45            default: {
46                break;
47            }
48        }
49    }
50    
51    /**
52     * If disconnected unsubscribe from Message Channel
53     */
54    disconnectedCallback() {
55        if (this.subscription) {
56            unsubscribe(this.subscription);
57        }
58    }
59}

While Lightning Message Service allows any component to publish a message on any channel, the Command Center global filter component does not subscribe to any LMS channels, so it won’t receive any messages published by other Command Center components.