[SalesForce] OpenTab with focus after closing some other tabs on Console Navigation Apps

I have overriden new Account and Edit Account standard actions with some custom Lightning Components. On console navigation apps i want to close some tabs on Account save and then open a new tab to the record id just created. But this does not seem to work and i do not understand the reason why.

navigateTo: function(component, recId) {
        //First determine whether navigation is Console or Standard
        var workspaceAPI = component.find("workspace");
        workspaceAPI.isConsoleNavigation().then(function(response) {
            if (response) { //navigation is console
                //Open a new tab with the record
                workspaceAPI.openTab({
                    recordId: recId,
                    focus: true,
                    overrideNavRules: true
                }).catch(function(error) {
                    console.log('error' +error);
                });
                workspaceAPI.getAllTabInfo().then(function(response) {
                    console.log(response);
                    for (var i=0; i<=response.length; i++) {
                        if ((response[i].title == 'Loading...') || (response[i].title == 'New Account') || (response[i].title == 'Νέος πελάτης') || (response[i].title == 'Φόρτωση...')) {
                            console.log(response[i].title);
                            workspaceAPI.closeTab({tabId: response[i].tabId});
                        }
                    }
                })
                .catch(function(error) {
                    console.log(error);
                });
            } else { //Standard navigation
                var navEvt = $A.get("e.force:navigateToSObject");
                navEvt.setParams({
                    "recordId": recId
                });
                navEvt.fire();
                window.setTimeout(
                    $A.getCallback(function() {
                        $A.get('e.force:refreshView').fire();
                    }), 2000
                );
            }
        })
    },  

The issue is, new tab opens, the tabs i want to close get closed, but focus is not on the opened tab by my code. It is one tab left from the tab opened by my code. Any Ideas ?

Best Answer

I also had this issue. But by using workspaceApi.focusTab() method, I was able to achieve the opening new tab and closing the current one and focusing the new tab.

workspaceAPI.openTab({
    pageReference: {
        "type": "standard__recordPage",
        "attributes": {
            "recordId": oldCaseId,
            "actionName":"view"
        }
    },
    focus: true
}).then(function(newTabId){
    workspaceAPI.getEnclosingTabId()
    .then(function(enclosingTabId) {
        workspaceAPI.closeTab({tabId : enclosingTabId});
        workspaceAPI.focusTab(newTabId);
    });

});

Hope this helps you. Let me know if it works.

Related Topic