Use Lightning Console JavaScript API methods in the JavaScript file of a Lightning web component or in the JavaScript controller of an Aura component.
LWC Syntax
To use LWC Workspace API, import lightning/platformWorkspaceApi in your JavaScript code.
The lightning/platformWorkspaceApi module gives you access to workspace API methods, wire adapters, and Lightning message channels. Access Lightning message channels by importing from @salesforce/messageChannel/lightning__tab*. For example, @salesforce/messageChannel/lightning__tabClosed.
1import{LightningElement, wire}from 'lwc';2import{EnclosingTabId, openSubtab}from 'lightning/platformWorkspaceApi';34export default class MyComponent extends LightningElement{5 @wire(EnclosingTabId)tabId;67 handleClick(){8 if(!this.tabId){9 return;10}11 // Open a record as a subtab of the current tab12 openSubtab(this.tabId, {recordId: 'YOUR_RECORD_ID', focus: true});13}14}
Where YOUR_RECORD_ID is a variable.
Configure the component’s .js-meta.xml file so the component can be accessed in the Lightning App Builder.
LWC supports Workspace API methods only. Similar to the Aura counterpart, methods in the Workspace API take a JSON object as an argument. The values included in the object depend on the method. For example, openTab takes an object that includes the url and focus (whether the new tab has focus). Check the reference section of this guide before using a method so that you know which arguments to pass to it.
Example
The lwc-recipes repo contains many LWC Workspace API examples. Look for components that start with workspaceApi, for example, workspaceAPICloseTab
Aura Components Syntax
To use the Lightning Console JavaScript API, include lightning:navigationItemAPI, lightning:workspaceAPI, or lightning:utilityBarAPI in your Aura component.
The lightning:navigationItemAPI, lightning:workspaceAPI, and lightning:utilityBarAPI components give you access to their coordinating APIs. Give each component an aura:id so that you can reference it from the component’s controller.
The following example shows a simple Aura component that uses the API libraries:
This component implements flexipage:availableForAllPageTypes so that it can be accessed in the Lightning App Builder.
The component’s JavaScript controller looks like this.
1({2 openUtilityBar : function(component, event, helper){3 var utilityAPI = component.find("utilitybar");4 utilityAPI.openUtility();5},67 openTab: function(component, event, helper){8 var workspaceAPI = component.find("workspace");9 workspaceAPI.openTab({10 pageReference:{11 "type": "standard__recordPage",12 "attributes":{13 "recordId":"500xx000000Ykt2AAC",14 "actionName":"view"15},16 "state":{}17},18 focus: true19}).then(function(response){20 workspaceAPI.getTabInfo({21 tabId: response22}).then(function(tabInfo){23 console.log("The recordId for this tab is: " + tabInfo.recordId);24});25}).catch(function(error){26 console.log(error);27});28},2930 focusNavigationItem : function(component, event, helper){31 var navigationItemAPI = component.find("navigationItem");32 navigationItemAPI.focusNavigationItem().then(function(response){33 console.log(response);34})35 .catch(function(error){36 console.log(error);37});38}39})
The controller has three functions, each of which uses an API method. To use a method in a controller, use component.find with the aura:id you gave to the lightning:navigationItemAPI, lightning:workspaceAPI, or lightning:utilityBarAPI.
Methods in the Workspace API and the Utility Bar API take a JSON object as an argument. The values included in the object depend on the method. openTab, for example, takes an object that includes the url and focus (whether the new tab has focus). Check the reference section of this guide before using a method so that you know which arguments to pass to it.
LWC VS Aura Guidelines
When working with the Lightning Console JavaScript API, consider these guidelines.
In LWC, required parameters are explicitly passed to the method like focusTab(tabId);. In Aura, required parameters are passed to the method in an object like workspaceAPI.focusTab({tabId : response});.
In LWC, pass in a URL that matches a Lightning Experience page, for example, /lightning/r/Account/001R0000003HgssIAC/view
Promises can simplify code that handles the success or failure of asynchronous calls. To use error handling with promises, use the catch() method on the promise that is returned from calling an API method.