function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DML2020DML2020 

How to reference recordId in controller of aura component?

A custom aura component is embedded in the screen element of a screen flow with the purpose of closing and opening specific tabs on the UI. The id of the tab to open in this case is defined as 'tabIdToOpen'.

From the code below, how should the markup appear for / what is the correct way to reference the recordId for the 'tabIdToOpen' in the controller file (line 7after recordId:)?

Component code 🔻

<aura:component implements="lightning:availableForFlowScreens">
    <aura:attribute name="tabIdToOpen" type="string" />
    <lightning:workspaceAPI aura:id="workspace" />
    <aura:handler name="init" value="{!this}" action="{!c.closeCurrentTab}" />
</aura:component>
 

Controller code 🔻

({
     closeCurrentTab : function(component, event, helper) {
          var workspaceAPI = component.find("workspace");
          workspaceAPI.getFocusedTabInfo().then(function(response) {
               var focusedTabId = response.tabId;
               workspaceAPI.openTab({
                    recordId: tabIdToOpen,
                    focus: true
               }).then(function(response) {
                    workspaceAPI.closeTab({tabId: focusedTabId});
               })
               .catch(function(error) {
                    console.log(error);
               });
          });
     }
})
Design Code
<design:component >
        <design:attribute name="tabIdToOpen" label="Id of tab to open" />
</design:component>
Reference -> openTab() for Lightning Experience (https://developer.salesforce.com/docs/atlas.en-us.api_console.meta/api_console/sforce_api_console_lightning_opentab.htm) sample code
 
SubratSubrat (Salesforce Developers) 
Hello ,

To reference the tabIdToOpen attribute in the controller file, you need to use the component.get() method. Here's the modified controller code:
({
    closeCurrentTab: function(component, event, helper) {
        var tabIdToOpen = component.get("v.tabIdToOpen"); // Retrieve the value of tabIdToOpen attribute
        
        var workspaceAPI = component.find("workspace");
        workspaceAPI.getFocusedTabInfo().then(function(response) {
            var focusedTabId = response.tabId;
            workspaceAPI.openTab({
                recordId: tabIdToOpen,
                focus: true
            }).then(function(response) {
                workspaceAPI.closeTab({ tabId: focusedTabId });
            })
            .catch(function(error) {
                console.log(error);
            });
        });
    }
})
In the modified code, component.get("v.tabIdToOpen") is used to retrieve the value of the tabIdToOpen attribute in the controller.

Make sure the markup for the component includes the attribute binding for tabIdToOpen. Here's an example:
<aura:component implements="lightning:availableForFlowScreens">
    <aura:attribute name="tabIdToOpen" type="String" />
    <lightning:workspaceAPI aura:id="workspace" />
    <aura:handler name="init" value="{!this}" action="{!c.closeCurrentTab}" />
</aura:component>
The design code you provided is already correct for defining the design attribute.

If this helps , please mark this as Best Answer.
Thank you.
DML2020DML2020

Thanks for the speedy reply, Subrat!

That component.get() method is key. Alternatively, I included it in my controller code as below (line 7) and it satisfied the requirement:

({
    closeCurrentTab: function(component, event, helper) {
        var workspaceAPI = component.find("workspace");
        workspaceAPI.getFocusedTabInfo().then(function(response) {
            var focusedTabId = response.tabId;
            workspaceAPI.openTab({
                recordId: component.get("v.tabIdToOpen"),
                focus: true
            }).then(function(response) {
                workspaceAPI.closeTab({ tabId: focusedTabId });
            })
            .catch(function(error) {
                console.log(error);
            });
        });
    }
})
 

🔴 Follow-up question: How is the example markup for the component above, to include the attribute binding for tabIdToOpen, different than what I originally posted?