function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dwayne JacksonDwayne Jackson 

Data not being received from LMS Channel

Hello,
I have two LWCs that are connected via an LMS channel.
LWC 1:
     - creates a custom record, and the related child records
     - publish a LMS channel and populate it with the ids
     - navigate to the record page the contains LWC 2  
LWC 2:
     - subscribes the LMS channel published by LWC1
     - retrieve the ids from the channel in connectedCallback
     - load the page with the custom record and related child record data

I can see the IDs in LWC 1 prior to navigating to the LWC 2. When LWC 2 loads connectedCallback is called but no ids are there. A snipet of the code is below. Any feedback on what I'm doing wrong?

// LWC 1 - A modal that contains the custom logic for creating a new Custom Object A record when the object +New Component Button is clicked
// - this component also does the following:
//     ** creates the related child component records
//     ** publishes an LMS channel with the Ids of the child components
//     ** navigates to the record page that contains the Custom Object A that was just created and the child component ids
import { LightningElement, api, wire } from 'lwc';
import { publish, MessageContext } from 'lightning/messageService';
import CUSTOM_OBJECT_A from '@salesforce/schema/Custom_ObjectA__c';
...
import MY_CHANNEL from '@salesforce/messageChannel/My_Id__c';

export default class CustomObjectACreator extends NavigationMixin(LightningElement) {
    @wire(MessageContext)
    messageContext;            
    
    customObjectApiName = CUSTOM_OBJECT_A;    
    
    @api recordId;  //The record Id set by the framework

    ...
    
    //Id's of Child Components. These are set when createRecord is executed in the child component 
    @api childComponent1RecordId = 'XXXXXX11111';
    @api childComponent2RecordId = 'YYYYYY22222';
    @api childComponent3RecordId = 'ZZZZZZ33333;  
       
    ...
    
     handleCreateCustomObject(event) {    

        //publish message with ids here...
        const payload = {
            childcomponent1recordid: this.childComponent1RecordId,
            childcomponent2recordid: this.childComponent2RecordId,
            childcomponent3recordid: this.childComponent3RecordId
        };        
        publish(this.messageContext, MY_CHANNEL, payload);  
        
         
        //Navigating to Record Page        
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: this.recordId,
                objectApiName: this.customObjectApiName,
                actionName: 'view'                
            }            
        });  
        
    }            
    
}


// This component is on the record page responsible for displaying the Custom Object A data with the related child components data
// This component also:
// - subscribes to the LMS channel published by LWC 1above
// - in connectedCallback, call the method to retrieve the related child component ids. NO IDs ARE PRESENT
// - load the related child components data based on the ids received from the LMS channel.
import { LightningElement, api, wire } from 'lwc';
...
import { subscribe, MessageContext } from 'lightning/messageService';
import CUSTOM_OBJECT_A from '@salesforce/schema/Custom_ObjectA__c';
...
import MY_CHANNEL from '@salesforce/messageChannel/My_Id__c';

export default class CustomObjectA extends LightningElement {
    
    ...
    //Id's for child components related to the Custom object 
    childComponent1RecordId;
    childComponent2RecordId;
    childComponent3RecordId;
 
    subscription = null;   
    
    //The Custom Object reference
    customObjectApiName = CUSTOM_OBJECT_A.fieldApiName;

    ...  
    
    connectedCallback() {        
        this.subscribeToMessageChannel();            
    }
    
    @wire(MessageContext)
    messageContext;

    // Encapsulate logic for LMS subscribe.        
    subscribeToMessageChannel() {
        this.subscription = subscribe(
            this.messageContext,
            SUBTASK_ID_CHANNEL,
            (message) => this.handleMessage(message)
        );        
    }    

    //Handler for message received by component. WHY CAN'T AM I NOT GETTING IDs HERE?
    handleMessage(message) {    
        console.log('CustomObjectA.handleMessage - childComponent Ids are: ', JSON.stringify(message));
        this.childComponent1RecordId = message.childcomponent1recordid;            //SHOULD BE: XXXXXX11111
        this.childComponent2RecordId = message.childcomponent2recordid;            //SHOULD BE: YYYYYY22222
        this.childComponent3RecordId = message.childcomponent3recordid;            //SHOULD BE: ZZZZZZ33333        
    }    
   ...    
    
}


 
Best Answer chosen by Dwayne Jackson
Dwayne JacksonDwayne Jackson
In my scenario I am attempting to navigate from an object page to a lighting record page. Confirmed through SF developer support that LMS requires that all components be contained within the same DOM. Context from an object page toa record page is not supported. Must use a ContextPageReference or apex controller classes.

All Answers

Naveen KNNaveen KN
check this 
https://www.codekiat.com/2020/08/Implement-lightning-message-service-lms-salesforce.html
Dwayne JacksonDwayne Jackson
In my scenario I am attempting to navigate from an object page to a lighting record page. Confirmed through SF developer support that LMS requires that all components be contained within the same DOM. Context from an object page toa record page is not supported. Must use a ContextPageReference or apex controller classes.
This was selected as the best answer