1

I want to iterate through the response of getAllTabInfo js function. I have the following function:

navigateTo: function(component, recId) {
        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
        );

        var workspaceAPI = component.find("workspace");
        workspaceAPI.getAllTabInfo().then(function(response) {
            console.log(response);
       })
        .catch(function(error) {
            console.log(error);
        });
    },

Under console.log(response);, i want to iterate through the results, get the tabids of the open tabs that have a specific title, and close those tabs. How can i achieve this ?

2

Your console log should give you all the information you need to loop through and close the tabs based on a title.

You can try something like this, and it will work, but please build off of this example, and don't just delete tabs based on a hardcoded tab title

var workspaceAPI = component.find("workspace");
workspaceAPI.getAllTabInfo().then(function(response) {
    console.log(JSON.parse(JSON.stringify(response))); // this will give you a better debug log.
    var tabTitleToDelete = 'Title to Delete' // Change this to the title string you want to delete...NOTE: this seems bad, it might be better to work on a dynamic way to do this, but this was your ask...
    for(var i = 0; i < response.length; i++) {
        var tabTitle = response[i].title; // title of the tab - usually the record "Name" field, if this is a "customTitle..."
        var customTitle = response[i].customTitle; // use this title if you have given a title to the tabs that you are wanting to close
        var tabId = response[i].tabId; // this is the id you'll need to call the closeTab function

        // You can add more customizations to this, like checking if this is a parent tab, or a subtab, or if it's focused, then not to close it
        // just use response[i].attributeName in your 'response' to figure this out.
        if(tabTitle === tabTitleToDelete) {
            workspaceAPI.closeTab({tabId: tabId});
        }

    }

})
.catch(function(error) {
    console.log(error);
});
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.