Forcetrails: Create Lightning message channel in easy and faster way


Hello Readers! In this post, I will show you how to create a Lightning Message Channel in easy steps so you can use that to communicate between LWC, VF, and Aura using Lightning Messaging Service. But before we go ahead you must-have VS Code and Salesforce CLI set up. So if you are ready with setup, let's get started.

  1. Set a salesforce project with VS Code, by running SFDX: Create Project or SFDX: Create Project with Manifest command in VS Code and then authorize your salesforce org with this project.

  2. Now if you have created the project with package.xml, then open your package.xml file and copy-paste the below code inside the <types> tag, so you can retrieve the existing messaging channels.

    <members>*</members>
    <name>LightningMessageChannel</name>
    

  3. Now go to your project's default folder
    force-app/main/default/

  4. Check if you have a folder named messageChannels, if not then create one folder under the default folder with the name messageChannels. Please use the provided name only, as this is the standard folder name.

  5. Now create a new file inside messageChannels folder with the following name format and give your desired name to the message channel.
    yourMessageChannelName.messageChannel-meta.xml

  6. Now open the file you just created in the previous step and copy-paste the below code in that. Please note that in the below file I have put the same name of my message channel from the last steps.

    Also, the lightningMessageFields defines the parameters that you can send using this message channel. It has two properties, fieldName, and description. You can add as many parameters as you want, but it's good to keep it optimum for better performance.

    For the demo, I have added two sample parameters recordId and recordData.

    <?xml version="1.0" encoding="UTF-8"?>
    <LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
    
        <masterLabel>YourMessageChannelName</masterLabel>
    	
        <isExposed>true</isExposed>
    	
        <description>This is a sample Lightning Message Channel.</description>
    	
        <lightningMessageFields>
            <fieldName>recordId</fieldName>
            <description>This is data parameter that you want to send through LMS</description>
        </lightningMessageFields>
    	
        <lightningMessageFields>
            <fieldName>recordData</fieldName>
            <description>This is another data parameter</description>
        </lightningMessageFields>
    	
    </LightningMessageChannel>
    

  7. Now as you have added your code and customized the parameters, just right-click the file yourMessageChannelName.messageChannel-meta.xml and select the deploy option to deploy the Message Channel.

  8. And that's it you are done. Please follow the steps 1 and 2 to retrieve the existing channels.


Call to action

Let me know in the comments if you find this helpful or you face any issues. Also do check my other posts about Lightning Web Components.


Further Reading

All posts about Lightning Web Components
LWC Series


No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, reach email us at rahul@forcetrails.com