2

I have added a Custom Lightining Component 'c:SF1ProductSearch' in a VF page. On my component i have added a Cancel button which should redirect to the Opportunity Record Detail Page in Salesforce1.

I am using force:navigateToSObject but i get am getting the error

"Uncaught error in $A.run() : Cannot read property 'setParams' of undefined"

Below is my Code:

VF Page:

<apex:page standardController="Opportunity" standardStylesheets="false" showHeader="false" sidebar="false" showQuickActionVfHeader="false">
    <link href="/resource/bootstrapsf/" rel="stylesheet"/>
     <apex:includeScript value="/lightning/lightning.out.js" />

    <div id="lightning" ></div>
       <script>       
        $Lightning.use("c:SF1AddOpportunityProduct", function() {
          $Lightning.createComponent("c:SF1ProductSearch", 
            { recordId: "{!opportunity.Id}" }, 
            "lightning",function(cmp) {
                //debugger;
            });
        });
    </script>  
</apex:page>

Lightining Component:

<ltng:require styles="/resource/bootstrap/bootstrap/dist/css/bootstrap1.css,
                          /resource/LighteningDesign/LighteningDesign/assets/styles/salesforce-lightning-design-system.css"
                  />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <!--Header Section -->
        <div class="panel-heading clearfix">
            <h4 class="panel-title pull-left" style="padding-top: 7.5px;">Search Products</h4>
            <div class="btn-group pull-right">
                <a href="#" class="btn btn-default btn-sm" onclick="{!c.goBack}">Cancel</a>
            </div>
        </div>

Client side Controller Function goBack

var oppid = component.get("v.recordId");
var navEvt = $A.get("e.force:navigateToSObject");
navEvt.setParams({
  "recordId": oppid,
});
navEvt.fire();

Grateful for any thoughts as to where im going wrong.

1 Answer 1

2

There are a couple of things going on here:

  • you would need to let the Lightning Framework know that you have a dependency on force:navigateToSObject by adding

    <aura:dependency resource="force:navigateToSObject" type="EVENT"/>

    to either your component or your LO app)

  • you are no longer running inside of S1/LEX directly and the container you are in (Visualforce) does not have any logic in it to respond to that event or even listen for it. No autoforwarding/mapping of events exists yet in Visualforce but I do have a story on the backlog for this very useful feature

The good news is that its not hard to write this yourself. Basically we just need a "shim" that listens for force:navigate* and bridges to the sforce.one.navigateTo* methods that already exist in VF when running under S1/LEX.

https://developer.salesforce.com/trailhead/lex_dev_visualforce/lex_dev_visualforce_navigation

If your component only needs to work in LC4VF you have an even easier path to just use the sforce.one.navigateToSObject() API directly:

if (sforce.one) {
   sforce.one.navigateToSObject(component.get("v.recordId"));
}
5
  • Hi Doug, Thanks for your response. When i add <aura:dependency resource="force:navigateToSObject"/> in my app/component , i am getting an error "Failed to save undefined: Invalid dependency *://force:navigateToSObject[COMPONENT]: Source" while Saving. Commented Oct 29, 2015 at 11:45
  • Hi Doug, Any Solution to it ? Commented Oct 30, 2015 at 12:43
  • i have added a dependency tag "<aura:dependency resource="markup://force:navigateToSObject" type="EVENT"/>" in my app.. This resolves the error on component. But nothing is happening on click of button. Commented Oct 30, 2015 at 14:29
  • force:navigateToSObject is a feature of Salesforce1 and Lightning Experience, and does not exist in Visualforce/Salesoforce Classis. Where are you trying to run the component?
    – Skip Sauls
    Commented Oct 30, 2015 at 17:37
  • force:navigateToSObject exists in LC in general - I think what Skip meant is that outside of S1/LEX nobody is listening for it out of the box. The specific issue above with the dependency is that I made a mistake - it should have read <aura:dependency resource="force:navigateToSObject" type="EVENT"/> . PLEASE NOTE: With that said please reread my answer - I think you would be better off using sforce.one.navigateToSObject() for now. Commented Oct 30, 2015 at 21:41

You must log in to answer this question.

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