TransWikia.com

Not able to use LMS in VF page iframe in Aura Component

Salesforce Asked by Krishna Teja Karnam on January 7, 2021

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"));
        }
    }
})

One Answer

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.

Correct answer by pchittum on January 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP