Newer Version Available

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

Lightning Console JavaScript API Syntax

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.

The following example shows a Lightning web component that uses the openSubtab API method and EnclosingTabId wire adapter.

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.

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