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).

To use LWC Workspace API (Beta), Lightning Web Security must be enabled in the Salesforce org. LWC Workspace API is a Beta Service. Customer may opt to try such Beta Service in its sole discretion. Any use of the Beta Service is subject to the applicable Beta Services Terms provided at Agreements and Terms.

Note

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}

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.