In this blog, I am going to how to use the background utility items that the runs from the background of utility bar of a console. Utilities harness the power of Lightning components. When you set up a utility bar, you select which Lightning components to use as utilities. However, not all Lightning components can be utilities. To be added to a utility bar, a Lightning component must implement the flexipage:availableForAllPageTypes interface. To run the utility bar from the background we need to implement the lightning:backgroundUtilityItem interface. lightning: backgroundUtilityItem is used to create a component that fires and responds to events without rendering in the utility bar. Background utility items are added to an app the same way normal utility items are, but they don’t appear in the utility bar. In this example, the component implements lightning:backgroundUtilityItem and listens for lightning:tabCreated events when the app loads. The component prevents users not to open more than 3 cases tab. If the user is trying to open the more than 3 tabs then it will close the first open tab automatically.
Step 1: Create a component
Here is the simple component that will run background. this component will run at the background and close the tab automatically when user trying to open the fourth tab.
<aura:component implements="lightning:backgroundUtilityItem,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" > <aura:attribute name="limit" default="3" type="Integer" /> <aura:handler event="lightning:tabCreated" action="{!c.onTabCreated}" /> <lightning:workspaceAPI aura:id="workspace" /> </aura:component>
({ onTabCreated: function(cmp) { var workspace = cmp.find("workspace"); var limit = cmp.get("v.limit"); workspace.getAllTabInfo().then(function (tabInfo) { console.log(tabInfo); if (tabInfo.length > limit) { workspace.closeTab({ tabId: tabInfo[1].tabId }); } }); } })
Step 2: Add a Utility Bar to Lightning Apps
- From Setup, enter App in the Quick Find box, then select App Manager.
- To edit the existing app, click Edit in the drop-down menu next to your Lightning App.
- Click the Utility Items tab and add the above created component to utility bar
Select the component and save it
Testing
go to the console and try to open the more than three cases now it will close the first tab automatically.
Key Consideration
When creating a utility bar for your app, keep these things in mind:
- Utility bars created using the Lightning App Wizard or in the Lightning App Builder can be assigned to only one Lightning app. However, utility bars created using the API can be assigned to multiple Lightning apps.
- The utility bar doesn’t support Visualforce pages or components.
- The utility bar doesn’t fully support the Chatter Publisher and Feed components.
- The History utility works in Lightning console apps only.
- The Omni-Channel utility works in the Lightning Service Console app only.