8

Is there any way to make force:navigateToSObject event to open sObject in new tab?

7

Yes, You can navigate to new tab using-

<aura:handler event="force:navigateToSObject" action="{!c.navigateToRecord}"/>

navigateToRecord : function(component , event, helper){
    window.open('/' + event.getParam('recordId'));  
},
  • From where do you fire this event? – Sarang Jul 29 '17 at 8:16
  • From a custom lightning component. – Ajay Prakash Dubey Jul 29 '17 at 8:17
  • You fire it and catch it in the same component. Probably be a good idea to cancel the event too – Caspar Harmer Jul 29 '17 at 8:17
  • 1
    I think you also need to add '_blank' as the second param to window.open() – Caspar Harmer Jul 29 '17 at 8:19
  • I knew the record Id, so when I directly used window.open('/' +recordId) it works. So is it OK to skip the navigate standard event? – Sarang Jul 29 '17 at 8:21
1

Since Salesforce suggests not using hard-coded or dynamic URLs, I would recommend using the more reliable lightning:navigation to open a new tab in Salesforce. I struggled with solving this at first, so here is my working solution. Hope it helps.

.cmp file:

//include this anywhere in your .cmp file
<lightning:navigation aura:id="navService"/>

controller.js :

//call your method/action
onClick: function(cmp, event, helper){
    var navService = cmp.find("navService");
    var pageReference = {    
       "type": "standard__recordPage", //example for opening a record page, see bottom for other supported types
       "attributes": {
       "recordId": recordId, //place your record id here that you wish to open
       "actionName": "view"
        }
    }

    navService.generateUrl(pageReference)
    .then($A.getCallback(function(url) {
      console.log('success: ' + url); //you can also set the url to an aura attribute if you wish
      window.open(url,'_blank'); //this opens your page in a seperate tab here
    }), 
    $A.getCallback(function(error) {
      console.log('error: ' + error);
    }));
}

According to the docs, pageReference supports opening:

  • Lightning Component (must implement lightning:isUrlAddressable)
  • Knowledge Article
  • Login Page
  • Named Page
  • Navigation Item
  • Page Object
  • Page Record
  • Page Record
  • Relationship Page
  • Web Page

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.