How to make force:navigateToSObject to open record in new tab
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'));
},
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