3

I want to use LMS to publish data from the VF page and receive it in Aura Component.

I have an Aura Component and am trying to iframe a Visualforce page in it.

<aura:component>
<iframe src='/apex/VFLMS'/>
</aura:component>

And am trying to publish the data to the Lightning Message channel from VFPage and subscribe to it in Aura Component.

When I see the output I get sforce is not defined.

I don't get the error when I try to use Visualforce standard component and drop the VF page into the standard component in the Lightning App Builder.

I have also made the VFPage be lightning available.

Here is my VF page

<apex:page lightningStylesheets="true">

    <!-- tried including api.js and it gives this error → Uncaught ReferenceError: Sfdc is not defined-->
    <button onclick="publishLeadData()"> Publish Msg</button>
       
    <script>

        var LEAD_DATA_CHANNEL = "{!$MessageChannel.LeadDataMessageChannel__c}";

        function publishLeadData() {

            const message = {
                recordId: '001XXXXXXXXXXX',
                name: "Testing"
            };
            sforce.one.publish(LEAD_DATA_CHANNEL, message);
        }
      
    </script>

</apex:page>

This is the Aura Component

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    
    <aura:attribute name="recordValue" type="String" />
    <lightning:messageChannel
                              type="LeadDataMessageChannel__c"
                              onMessage="{! c.handleLeadDataMessage }"
                              
                              />
    <iframe src="/apex/LMSVF" frameborder="0" />
    
    {! v.recordValue }
</aura:component>

And this is the controller.js file

({
    handleLeadDataMessage : function(component, event, helper) {
        if (event != null && event.getParams() != null) {
            let params = event.getParams();
            console.log(JSON.stringify(params));
            component.set("v.recordValue", JSON.stringify(params, null, "\t"));
        }
    }
})
2
  • I don't have an answer but thought I'd simplify the question for other who come here. I think what you're really trying to ask is: "I have an Aura component that uses an iFrame to include a Visualforce page. That Visualforce page does not have access to the sforce.one object. Is there a way to give that VF page access to the sforce.one object so that it can publish to LMS? I do not want to use the undocumented isdtp=p1 query parameter. Alternatively, if there is no way for the VF page to access the sforce.one object, is there any other way to publish to LMS from a VF page?" Aug 31, 2020 at 20:44
  • Exactly @JoeFlowers Thanks for summarising it. Sep 1, 2020 at 4:36

2 Answers 2

5

As per the docs, "Lightning Message Service does not work in Visualforce pages that are included in Lightning Experience via iframes, including <wave:dashboard>, <apex:iframe>, and the standard HTML <iframe> tag.

The intent of LMS isn't to serve as a shortcut for a VF page embedded in an iFrame to send a message to its immediate container. Rather, it's purpose is for solving the harder problem of cross-DOM-cross-ui-technology messaging.

-1

As mentioned in the above post we can't use LMS in this situation, a workaround for this would be using <HTMLREFERENCETOIFRAME>.contentwindow.postMessage(). Please refer to these posts - https://sfwiseguys.wordpress.com/2020/08/10/lwc-with-vf-in-an-iframe-bidirectional-communication/#:~:text=LWC%20to%20VF%20%E2%80%93,pass%20to%20the%20other%20window.,

https://javascript.info/cross-window-communication

1
  • Can you add some relevant content from those links? Links on their own tend to break over time
    – Matt Lacey
    May 5, 2021 at 2:12

You must log in to answer this question.

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