2

force:editRecord event works fine in my lightning app, but the same does not work in the visual force page. I am aware that force:editRecord is handled by one app container but I am looking for work around here like using $A.eventService.addHandler.

Visualforce page

<apex:page showheader="false" sidebar="false" standardStylesheets="false">
    <html>

        <body>

            <apex:includeLightning />
            <div id="lightning"/>

            <script>
            var myUserContext = "{!$User.UITheme}";
            $Lightning.use("c:TestApp", function() {
                $Lightning.createComponent("c:xyz",
                                           {},
                                           "lightning",
                                           function(cmp) {
                                               $A.eventService.addHandler({
                                                   event:'force:editRecord',
                                                   handler: function(event { 
                             varlexOrigin="https://xyz.lightning.force.com";
                                var message=
                                         {type: "EventFromVF",                                                                 
                                    message: event.getParams().recordId};


                                   parent.postMessage(message, lexOrigin);
                                                       }
});

                                           }
                                          );
            });    
            </script>

        </body>
    </html> 
</apex:page>

lightning source from edit record being called
======================
edit:function(component, event, helper) {
            var editRecordEvent = $A.get("e.force:editRecord");
            editRecordEvent.setParams({"recordId": component.get("v.record.Id")});
            editRecordEvent.fire();

Listener controller and component

<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
    <aura:attribute name="vfHost" type="String"
        default="xyz.visual.force.com"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <iframe aura:id="vfFrame" src="{!'https://' + v.vfHost + '/apex/xyzpage'}" />
</aura:component>

({
    doInit : function(component) {
        debugger;
        console.log('hello listener');
        var vfOrigin = "https://" + component.get("v.vfHost");
        window.addEventListener("message", function(event) {
            if ( (event.data.type) && (event.data.type=='EventFromVF') )
            if (event.origin !== vfOrigin) {
                // Not the expected origin: Reject the message!
                return;
            }

            // Handle the message
            var toastEvent = $A.get('e.force:editRecord');
            toastEvent.setParams({
                recordId: event.data.message
            });
            toastEvent.fire();

            console.log(event.data);
        }, false);
    }

})

lightning application TestApp

<aura:application extends="ltng:outApp"  access="GLOBAL" >    
    <aura:dependency resource="c:xyz" />
    <aura:dependency resource="markup://force:editRecord" type="EVENT"/>
</aura:application>
  • 1
    It's not clear exactly what you're trying to do. Adding specificity to your question and including representative extracts of the code you're working on may help produce a solution. – David Reed Aug 14 '18 at 13:33
  • Hi David, thanks for your response. Actually, I have used force:editRecord event in my lightning component and the event works as expected if I use the component in my lightning application but when I am using that component in my visual force page that event is not getting fired which works fine from the lightning application.force:editRecord is handled by one app container but I am looking for work around to make it work inside visual force page,hope this makes it clear, let me know if it is still not clear – Chandra Teja Aug 14 '18 at 13:39
  • I believe it would be pertinent for you to include how you are trying to make it work in Visualforce – glls Aug 14 '18 at 13:53
  • 1
    Hi glls, thanks for your input. please check now i have shared the code – Chandra Teja Aug 14 '18 at 14:15
0

e.force:editRecord is not supported from VisualForce using lightning out - you can see from the documentation - at the bottom of the page:

This event is handled by the one.app container. It’s supported in Lightning Experience, the Salesforce app, and Lightning communities.

That means it is not supported from lightning out as you are trying to use it

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.