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
steve nabors 7steve nabors 7 

Problems with using a Utility Bar custom lightning component with recordId, it is undefined in the controller.

I'm having a problem passing the recordId to the controller of my lightning component when that component is on the utility bar.  I can see the recordId just fine in the component, but when I access it from the controller, I get undefined.  If I add the very same component to a record page, it works fine.  I tried a few different things just using some basic code:
Component:
<aura:component implements="flexipage:availableForAllPageTypes, force:hasRecordId">
    <aura:attribute name="TestString" type="String" access="global" default="Test String" />
    <aura:attribute name="TestRecordId" type="String" access="global" default="{!v.recordId}" />
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <p>The RecordId is: {!v.recordId}</p>	
    <p>The TestRecordId is: {!v.TestRecordId}</p>	
    <p>The TestString is: {!v.TestString}</p>	
</aura:component>

Controller:
({
    init : function(component) {
        console.log("RecordID: " + component.get("v.recordId"));
        console.log("TestRecordID: " + component.get("v.TestRecordId"));
        console.log("TestString: " + component.get("v.TestString"));
    }
})

When on a Record Page:
RecordPage
When on the Utility Bar:
Utility Bar
AnudeepAnudeep (Salesforce Developers) 
Hi Steve - I reported this internally and our product team has replied with the following

Thank you for reporting this issue and providing the simple code to reproduce. We were able to reproduce the issue and see where this is happening in the code. It is caused by the lifecycle of the Utility Bar. We retrieve the components from a flexipage, dynamically create them, and then set the recordId property after they are created. This is why you can see the recordId is correctly displayed on the Utility Item panel, but not in your init() console logs. I think it has always worked this way, unfortunately
 
A workaround would be to grab the recordId in the afterRender() instead of init(). We have done that in the screenshot below.

User-added image
 
If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep