4

In Lightning Experience, I have a custom URL button on Opportunity object that references a Visualforce page that surfaces a Lightning Component.

The Lightning Component has a "Cancel" button that when clicked I want to navigate back to the Opportunity record.

The challenge I'm having is I'm not sure how to do this.

MyComponent.cmp

<lightning:button aura:id="cancelButton"
                  name="cancelButton"
                  label="Cancel"
                  variant="neutral"
                  onclick="{!c.onClickCancelButton}"/>

MyComponentController.js

onClickCancelButton : function(component, event, helper) {

    console.log( 'onClickCancelButton' );

    var recordId = component.get( 'v.recordId' );

    var event = $A.get( 'e.force:navigateToSObject' );

    if ( event ) {

        // I never get here, the event isn't available
        // from Visualforce context even though I'm rendering in LEX

        event.setParams({
            'recordId' : recordId
        }).fire();

    } else {

        // since no event to fire for navigation, what else can I do?

        // LockerService blocks modifying window.location

        // Monkeying with window.location.pathname only loads a Classic page nested in LEX


    }

}

Lightning Out Visualforce Page

<apex:page>

    <apex:includeLightning />

    <div id="container"/>

    <script>

        $Lightning.use( "c:MyLightningOutApp", function() {

            var recordId = '{!$CurrentPage.parameters.recordId}';

            $Lightning.createComponent(
                'c:MyComponent',
                {
                    'recordId' : recordId
                },
                'container',
                function( component ) {

                }
            );

        });

    </script>

</apex:page>
5
  • 1
    does any of this help, maybe the last part?? developer.salesforce.com/docs/atlas.en-us.salesforce1.meta/…
    – Eric
    Commented Nov 22, 2016 at 4:00
  • 1
    Yes .Try this sforce.one.navigateToSObject(recordId) .Since you are inside Sf1 container the Sforce Should be available if not force events . Commented Nov 22, 2016 at 4:03
  • @MohithShrivastava never in a million years would I have thought I could use sforce.one in this visualforce page. I guess it makes sense... wow. thanks!
    – Doug Ayers
    Commented Nov 22, 2016 at 4:06
  • 2
    @DougAyers - Done. Also see my linked answer for what I have used for a SLDS template across all my transitioning VF pages. Just in case it can be of use to you.
    – Eric
    Commented Nov 22, 2016 at 4:11
  • Another solution is here: salesforce.stackexchange.com/questions/152428/…
    – Joe B
    Commented Dec 22, 2016 at 16:30

2 Answers 2

6

I have found the following to work in my VF pages when transitioning to LEX:

(function(myContext){
    myContext.ForceUI = myContext.ForceUI || {};

    myContext.ForceUI.isSalesforce1 = function() {
        return((typeof sforce != 'undefined') && sforce && (!!sforce.one));
    }
})(this);

Then you can use (as an example):

ForceUI.isSalesforce1() ? navigateToSObject(aId) : (window.location.href='/' + aId);

https://developer.salesforce.com/docs/atlas.en-us.salesforce1.meta/salesforce1/vf_dev_best_practices_pages_multipurpose.htm

Also, see my answer here for a template to use to navigate as needed and make it easy to transition your VF pages:

What is suggested approach to transfer VF pages to be lightning ready

1

The solution can be found in an answer to a related question: How to navigate in lightning out? This answer references the event force:navigateToObjectHome and its corresponding sforce.one method navigateToObjectHome which is not exactly what you need. Instead, you'll want to raise the event force:navigateToSObject from your cancel button click and handle it by calling sforce.one.navigateToSObject() from your VF page.

Also, the code $A.get( 'e.force:navigateToSObject' ) returns null because you haven't added the force:* events as a dependency in your dependency app.

For a more in-depth look at Lightning navigation in Visualforce, check out the blog post Salesforce Lightning - Navigation Events in a Visualforce Page.

You must log in to answer this question.

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