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 controller of a Lightning component.

To use the Lightning Console JavaScript API, include force:workspaceAPIAccess, force:utilityBarAPIAccess, or both in your Lightning component.

The force:workspaceAPIAccess component gives you access to the workspace API, while the force:utilityBarAPIAccess component gives you access to the utility bar API. Give each component an aura:id so that you can reference it from the component’s controller.

The follow example shows a simple Lightning component that uses both libraries:

1<aura:component implements="flexipage:availableForAllPageTypes" access="global">
2    <force:workspaceAPIAccess aura:id="workspace" />
3    <force:utilityBarAPIAccess aura:id="utilitybar" />	
4      
5    <lightning:button label="Open Utility" onclick="{! c.openUtilityBar }"/> 
6    <lightning:button label="Open Tab" onclick="{! c.openTab }" /> 
7</aura:component>

This component implements flexipage:availableForAllPageTypes so that it can be accessed in the Lightning App Builder.

This is the component’s JavaScript controller:

1({
2    openUtilityBar : function(component, event, helper) {
3        var utilityAPI = component.find("utilitybar");
4        utilityAPI.openUtility();
5    },
6    
7    openTab : function(component) {
8        var workspaceAPI = component.find("workspace"); 
9        workspaceAPI.openTab({
10            url: '#/sObject/001R0000003HgssIAC/view', 
11            focus: true,
12            });
13    }, 
14})

Utility bar API methods work only when called from the utility bar. The openUtilityBar method in this example works when it is part of a one-column Lightning page added to a Lightning app’s utility bar.

Note

The controller has two 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 force:workspaceAPIAccess or force:utilityBarAPIAccess.

Methods in the Utility Bar API take values directly. Methods in the Workspace API take a JSON array as an argument. The values included in the array depend on the method. openTab, for example, takes an array 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.

Using Callback Functions

Many methods in the Lighting Console JavaScript API return results asynchronously, in callback methods. This example uses the workspace API function getFocusedTabId() to get the tab ID of the focused tab. Then, the callback function calls closseTab() and closes the focused tab.

1getFocusedTabInfo : function(component, event, helper) {
2    var workspaceAPI = component.find("workspace");
3    workspaceAPI.getFocusedTabInfo({
4       callback : function(error, response){
5                var focusedTabId = response.tabId; 
6                workspaceAPI.closeTab({tabId : focusedTabId});
7            }
8        });
9},