Newer Version Available

This content describes an older version of this product. View Latest

focusTab() for Lightning Experience

Focuses a workspace tab or subtab. This method works only in Lightning console apps.

Arguments

The method provides the same argument for both Aura Components and Lightning Web Components (LWC).

Name Type Description
tabId string ID of the workspace tab or subtab on which to focus.

LWC Sample Code

This component has a function that opens and focuses a tab that’s retrieved by a page reference.

1import { LightningElement } from 'lwc';
2import { openTab, focusTab } from 'lightning/platformWorkspaceApi';
3
4export default class MyComponent extends LightningElement {
5
6    focusNewTab(event) {
7        openTab({
8            url: '/lightning/r/Account/001R0000003HgssIAC/view',
9            label: 'Global Media'
10        }).then((tabId) => {
11            focusTab(tabId);
12        }).catch((error) => {
13            console.log(error);
14        });
15    }
16}

Example

The lwc-recipes repo has a workspaceAPIFocusTab component that demonstrates usage of the focusTab() method.

Aura Components Sample Code

This component has a button that, when pressed, opens a new tab and focuses it.

Component code:

1<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
2    <lightning:workspaceAPI aura:id="workspace" />
3    <lightning:button label="Focus New Tab" onclick="{! c.focusNewTab }" />
4</aura:component>

Controller code:

1({
2    focusNewTab : function(component, event, helper) {
3        var workspaceAPI = component.find("workspace");
4        workspaceAPI.openTab({
5            url: '/lightning/r/Account/001xx000003DI05AAG/view',
6        }).then(function(response) {
7            workspaceAPI.focusTab({tabId : response});
8       })
9        .catch(function(error) {
10            console.log(error);
11        });
12    }
13})

The relative URL used in this example is a placeholder. To try this example yourself, use a relative URL with a record ID from your org.

Note

Response

This method returns a promise that, upon success, resolves to true.