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.
-
Set a salesforce project with VS Code, by running
SFDX: Create Project
orSFDX: Create Project with Manifest
command in VS Code and then authorize your salesforce org with this project. - Now if you have created the project with
package.xml
, then open yourpackage.xml
file and copy-paste the below code into the<types>
tag, so you can retrieve the existing messaging channels.
<members>*</members> <name>LightningMessageChannel</name>
-
Now go to your project's default folder
force-app/main/default
/
-
Check if you have a folder named
messageChannels
, if not then create one folder under the default folder with the namemessageChannels
. Please use the provided name only, as this is the standard folder name.
-
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
-
Now open the file you just created in the previous step and copy-paste the below code. Please note that in the below file, I have put the same name of my message channel from the last steps.
Also, thelightningMessageFields
defines the parameters that you can send using this message channel. It has two properties,fieldName
, anddescription
. 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 parametersrecordId
andrecordData
.
<?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>
-
Now that 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.
- And that's it you are done. Please follow the steps 1 and 2 to retrieve the existing channels.
Let me know in the comments if you find this helpful or if you face any issues. Also, do check my other posts about Lightning Web Components.
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, email us at rahul@forcetrails.com