Newer Version Available
openTab() for Lightning Experience
Opens a new workspace tab that displays the content of a specified URL, which can be
relative or absolute. If the tab is already open, it is focused. This method works only in
Lightning console apps.
Arguments
| Name | Type | Description |
|---|---|---|
| recordId | ID | Specifies the record to open. If you specify a record ID, the value for url isn’t required and is ignored. |
| url | URL |
The URL representing the content of the new workspace tab. The URL can be either relative or absolute. If you specify a URL, a record ID isn’t required. If a record ID is specified, the
url value is ignored.
|
| focus | boolean | Specifies whether the new tab has focus. |
| overrideNavRules | boolean | Optional. Specifies whether the open tab respects existing navigation rules. This argument has no effect on records that have no navigation rules configured and URLs that do not point to a record. |
Sample Code
This component has a button that when pressed, opens a tab.
Component code:
1<aura:component implements="flexipage:availableForAllPageTypes" access="global">
2 <lightning:workspaceAPI aura:id="workspace"/>
3 <lightning:button label="Open Tab" onclick="{!c.openTab}"/>
4</aura:component>Controller code (recordId):
1({
2openTab : function(component, event, helper) {
3 var workspaceAPI = component.find("workspace");
4 workspaceAPI.openTab({
5 recordId: "001xx000003DIkeAAG",
6 focus: true
7 }).then(function(response) {
8 workspaceAPI.focusTab({
9 tabId: response
10 });
11 })
12 .catch(function(error) {
13 console.log(error);
14 });
15 }
16})Controller code (url):
1({
2 openTab : function(component, event, helper) {
3 var workspaceAPI = component.find("workspace");
4 workspaceAPI.openTab({
5 url: "#/sObject/500R0000000myfGIAQ/view",
6 focus: true
7 }).then(function(response) {
8 workspaceAPI.focusTab({
9 tabId: response
10 });
11 }).catch(function(error) {
12 console.log(error);
13 });
14 }
15})Response
This method returns a promise that, upon success, resolves to the
tabId of the new tab.