Close All Salesforce Console Tabs when Logging in

Author: Bruce Tollefson Published: April 28, 2021; Modified: May 22, 2022

The great thing about console apps is the ability to have multiple tabs open at once. This is important for many reasons, one of them being CTI. As great as they are those tabs can very quickly pile up and a user can have a large amount of tabs open at once. While you are in Salesforce you can use the shortcut Shift + W. However, what if your users want to be able to start out fresh when they log in and start; they don’t want all their previous tabs open? Or what if there is a compliance reason for closing all tabs?

There are 2 ways to accomplish this; thanks to the Spring ’22 release there is now an option to do this out of the box. Within the app setting, go to any console app in app manager. Under Setup and Personalization, select App Personalization Settings. Select Clear workspace tabs for each new console session. The other way is more custom which will be the focus for the rest of the article in case the out of the box functionality does not behave as you thought it would or if you wanted to provide the user the ability to pick whether the tabs are to be closed.

With a custom field on the user, a login flow, an aura utility component, and an apex controller class you can remove all tabs on a console app when a user logs in. This design could be modified for each time the user refreshes but that may be a little too much and if you are using CTI they shouldn’t be refreshing. The custom field will be checked each time the login flow will be fired which in turn will trigger the aura component to get all of the open tabs(since open tabs stay open even when logging out) and close all tabs.

The first step will be to create the custom field on user:

  • Label
  • Data Type
  • Close Tabs
  • Checkbox

Now create the apex controller:

Current User Apex Controller

This controller will get the current user, return the value of the close tabs field, and uncheck the field. This way if the user does refresh or closes the browser on accident and clicks in with the same session nothing will be closed.

public class currentUserController { @AuraEnabled public static Boolean closeTabsMethod(){ boolean closeTab; User currentUser = [Select Id, Close_Tabs__c from User where Id = :UserInfo.getUserId()]; closeTab = currentUser.Close_Tabs__c; if(closeTab){ currentUser.Close_Tabs__c = false; } update currentUser; return closeTab; } }

After the controller is created time to hook it up to an aura component.

Close Tabs Aura Component

In order to get this to work we need to use the workspaceAPI at the time of this post unfortunately the workspaceAPI can only be used with aura, otherwise this would be an LWC.

Close Tabs Utility CMP

<aura:component implements="lightning:backgroundUtilityItem,lightning:utilityItem" controller="currentUserController"> <aura:handler name="init" value="{!this}" action="{!c.closeAllTabs}"/> <lightning:workspaceAPI aura:id="workspace" /> </aura:component>

Pretty straight forward this will be a background utility calling the controller on the init.

Close Tabs Utility JS

In the javascript call the controller, get all the open tabs, loop through the parents and close.

({ closeAllTabs: function (cmp, evt, hlp) { const action = cmp.get("c.closeTabsMethod"); const workspaceAPI = cmp.find("workspace"); action.setCallback(this, function(response) { let state = response.getState(); if (state === "SUCCESS") { if(response.getReturnValue()){ workspaceAPI.getAllTabInfo().then((response) => { //console.log(response); if (!response) return; => { //console.log(singleTab); if(singleTab.closeable && !singleTab.isSubTab){ let tabId = singleTab.tabId; workspaceAPI.closeTab({tabId}); } }); }); }else { console.log("Close Tabs: " + response.getReturnValue()); } } else { console.log("Failed with state: " + state); } }); workspaceAPI.isConsoleNavigation().then(function(response) { if(response){ $A.enqueueAction(action); } }) .catch(function(error) { console.log(error); }); }, })

After creating the component create the login flow.

Close Tabs Login Flow

Unfortunately login flows will need to be a screen flow, otherwise this would be in the background as well. So if you need to put in any type of reminder on login this would be a good place. Also from the help page NOTE You can’t apply login flows to API logins or when sessions are passed to the UI through frontdoor.jsp from a non-UI login process. Create a screen flow that will update the current user record:

Simple flow

Update the User record based on the login flow user Id variable.

Create the LoginFlow_UserId variable this will get the current user Id.

Activate the screen flow.

After activating the screen flow, create the login flow through Setup –> Login Flows –> New. This will need to be repeated if you are planning to use this for multiple profiles as it can not be left blank.

Now all you will need to do is add the aura component to any console navigation app you would like automatically have the Salesforce console tabs close when logging in.

4 comments on “Close All Salesforce Console Tabs when Logging in”

  • Hairstyles VIP says:

    whoah this blog is excellent i love studying your articles. Stay up the great work! You understand, lots of people are looking round for this information, you can help them greatly.

  • ko213 says:

    Absolutely brilliant tutorial, its amazing how in Lightning its not a feature. Thank you so much!

  • Leave a Reply

    Your email address will not be published. Required fields are marked *