0

I have a aura component call myComponent that opens a tab with WorkspaceAPI:

myComponent

myComponent : function(component) {
        var valueName = component.get("v.valueName");
        var recordId = component.get("v.recordId");
        var orderNumber = component.get("v.orderNumber");     

var pageReference = {
        "type": "standard__component",
        "attributes": {
            "componentName": "c__tabComponent"  
        }, 
        "state": {
                "c__name": valueName,
                "c__id": recordId,
            }
        };

     var workspaceAPI = component.find("workspace");
        workspaceAPI.openTab({
            pageReference: pageReference,
            focus: true
        }).then((response) => {
               workspaceAPI.setTabLabel({
                  tabId: response,
                  label: "Order: " + orderNumber
               });
               workspaceAPI.setTabIcon({
                tabId: response,
                icon: "standard:orders"
               });
        }).catch(function(error) {
            console.log(error);
        });
   }

The tabComponent is open in a tab and I want to closed the tab trough a button, but I tried several things to send a value to myComponent to fire this method so he can close with this code, because I don't have the value of tabId on tabComponent and also I don't have the lightning:workspaceAPI, because it's in the myComponent:

closeTab : function(component) {
  var workspaceAPI = component.find("workspace");
  workspaceAPI.closeTab({tabId: component.get("v.tabId")});
}

How can I close the tab with a button in tabComponent? I don't have the tabId and also the lightning:workspaceAPI, they are not parent and child also :(

1 Answer 1

0

So, I was able to figure it out based on the salesforce document, to someone that it's passing through this same problem, here's the solution:

closeTab: function(component) {
        var workspaceAPI = component.find("workspace");
        workspaceAPI.getFocusedTabInfo().then(function(response) {
            var focusedTabId = response.tabId;
            workspaceAPI.closeTab({tabId: focusedTabId});
        })
        .catch(function(error) {
            console.log(error);
        });
    }

You have to add this also to the component that was open with workspaceAPI:

<lightning:workspaceAPI aura:id="workspace"/>
1
  • Instead of focused tab id, you should use getEnclosedTabId. This is because there may be instances where the method is called, but the user makes an immediate switch to another tab, and the current focused tab will get picked up. The delay is seen where there are multiple operations happening on UI. Apr 24 at 2:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .