1

I'm trying to use the lightning message service on a visualforce page.

To simplify things, I'm using the exact code supplied by Salesforce with the one exception I've updated the SAMPLEMC variable to use my message channel. Ref: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/message_channel_publish.htm

<apex:page >
  <script>
    // Load the MessageChannel token in a variable
    var SAMPLEMC = "{!$MessageChannel.MY_NAMESPACE__Awesome_LMS__c}";
    function handleClick() {
      const payload = {
        recordId: "some string",
        recordData: {value: "some value"}
      }
      sforce.one.publish(SAMPLEMC, payload);
    }
  </script>

  <div>
    <p>Publish SampleMessageChannel</p>
    <button onclick="handleClick()">Publish</button>
  </div>
</apex:page>

When I click the button, I get the Uncaught ReferenceError: sforce is not defined in the console:
enter image description here

This is similar to Lightning Messaging Service: sforce.one not defined however my VF page is not running in an iframe.

I've attempted this in both Classic and Lightning UI.

Any ideas what the problem is, and how to solve?

1 Answer 1

1

You can only use LMS in Visualforce in certain conditions. Make sure you're using it in a compatible manner, or you'll run in to problems.

I've included the limitations here.

Keep the following in mind when working with Lightning message service in Visualforce.

Considerations

  • Lightning message service doesn’t work for Visualforce's sforce.one library when pages are loaded in the Chatter Publisher that uses <chatter:feed showPublisher="true"/>. Use the native Lightning Publisher instead.
  • Lightning message service doesn’t work in Visualforce pages that are included in Lightning Experience via iframes, including <wave:dashboard>, <apex:iframe>, and the standard HTML <iframe> tag. Instead, add Visualforce pages through the Lightning App Builder or as a utility bar item.
  • Visualforce supports only LightningMessageChannels where isExposed is true. See LightningMessageChannel in the Metadata API Developer Guide for more information.
  • Lightning message service doesn’t work in Salesforce Classic or when previewing Visualforce from Setup.

Limitations

The lightning message service supports only the following experiences:

  • Lightning Experience standard navigation
  • Lightning Experience console navigation
  • Salesforce mobile app for Aura and Lightning Web Components, but not for Visualforce pages
  • Lightning components used in Experience Builder sites. Support for Experience Builder sites is beta.

Note

Lightning Message Service doesn't work with Salesforce Tabs + Visualforce sites or with Visualforce pages in Experience Builder sites.

In other words, you can only use LMS in certain situations, such as a Lightning Utility Bar that has Visualforce in it. For other cases, you may need to use pubsub or another homebrew variant.

2
  • Hey @sfdcfox, could you please guide how VisForce needs to be added in an Lightning Utility Bar so that it would support the LMS. I see how to add it as part of an iframe, but not without an iframe. Could you please suggest what to read? Is it needs to be just a VF page or VF component is the required way? Commented Jan 17 at 15:23
  • I found it> in the UtilityBar configuration I choose VisualFOrce page, then configure it:) Commented Jan 17 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .