Select Page

Early 2020 saw Salesforce release “Lightning Message Service“ (LMS), a new solution that enables developers to interact smoothly between VF and Lightning. It cuts down the time to interact between platforms by giving a fast channel to which consumers can subscribe and get updates, so developers don’t have to bother about complex Javascript.

Getting Started

Before beginning code, we have to build a Message Channel. The only method to do this is by leveraging the Metadata API. Salesforce will ultimately provide a friendly UI for it, but we have to do a little work right now.

Initially, we will build a zip file containing both a package.xml and a file describing the name and fields for our Message Channel. This file should be within a folder called messageChannels, and the setup for the filename is CHANNELNAME.messageChannel.

In our instance, that means SayWhat.messageChannel.

Go to the subsequent screen in workbench, click “Deploy,” and then obey the commands. In the end, you’ll have an appropriately deployed Message Channel!

Visualforce

We’re going to manage Visualforce to begin with. In the markup, we will install a simple textbox/button order to transfer messages and two buttons/textbox to manage the subscribe/unsubscribe and decoding of messages.

Then, below, we’ll add the Javascript to make this all transpire. The initial action is to name a couple of variables that will be used throughout the sample. Remark the use of $MessageChannel, a new object available in VF that allows us to get to the correct GUID.

To declare a message to the channel, we build a JS function that produces a JSON object, and then we call the sforce.one.publish process with it as a parameter. My JSON object features agree with the names of the fields involved in the Message Channel, and that none of them uses __c.

To subscribe and unsubscribe, we use a related method. We make a call to a process in the sforce.one namespace. The subscribe process returns an object that we will require to unsubscribe later. The parameter on that event will include a JSON object with the same shape.

Aura

Aura is a little distinctive from VF. Salesforce did not add a subscribe/unsubscribe functionality here; instead, of automatically doing it when we place the lightning:messageChannel segment on the page. The onMessage event will let us understand when something has been published.

To publish, we simply create a JSON object and transfer it to the publish method of the segment we defined in markup.

The JSON’s parts come as the event variable’s parameters, so we have to use getParam to see the values into variables. Using event.propertyname will yield an undefined value.

In review, LMS presents us with a rapid and straightforward way to move complicated data across the DOM between VF and LWC/Aura, enabling greater interoperability. There are several cost-saving possibilities in retrofitting old VF pages to run with new Lightning elements.

Submit a Comment

Your email address will not be published. Required fields are marked *

Submit a Comment

Your email address will not be published. Required fields are marked *

Let’s Start Something New

    Contact-form-image.jpg

    You can also email us directly at contact@criticalriver.com