Hi I'm having an issue initializing a lightning component that is loaded in my app's utility bar. It has a refresh button that calls the doInit function that works fine but trying with the following line doesn't work:

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

The problem is the attribute 'recordId' hasn't populated yet and returns undefined. The doInit function starts off like this:

doInit : function(component, event, helper) {
    var caseId = component.get("v.recordId");
    if(caseId && caseId != null){

caseId returns undefined when being called from the init handler but not from the refresh button. The component implements force:hasRecordId. I've tried using aura:render and aura:doneRendering but it recursively calls the function and gets stuck infinitely.

Is there a handler for when it's done loading or done receiving all it's attributes?

Here's how the lightning component is being added to the app. And recordId is being accessed through the force:hasRecordId implementation. https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation

app setup

  • Is that component on recordDetail page? Apr 12 '19 at 16:29
  • 1
    init loads after all the props are populated. So something else might be going on. Could we see more of the component in question and how it's called? Apr 12 '19 at 16:43
  • What's the context for this component? force:hasRecordId only works at the top level. If you're using a Lightning Console, for example, you need to listen for tab events. If it's an app, you need to make sure you pass in the record Id, etc.
    – sfdcfox
    Apr 12 '19 at 16:55
  • Navigation style is set to console navigation. So yeah, usually the attribute would be passed in from whatever parent page created the component. So like <c:checklist recordId="{!v.recordId}" or whatever. But here it's just being added as a utility item on the apps' setup screen so I don't have access to the parent component's code. Instead recordId is being passed through the force:hasRecordId implementation. developer.salesforce.com/docs/component-library/bundle/…
    – Dylan
    Apr 12 '19 at 17:58

In my experience with aura, anything involving Lightning Data Service has to be considered to be an async operation, so you need to be looking for the recordUpdated event rather than trying to use recordId in init - per the docs:

"force:recordData loads data asynchronously by design since it may go to the server to retrieve data. To track when the record is loaded or changed, use the recordUpdated event as shown in the previous example. Alternatively, you can place a change handler on the attribute provided to targetRecord or targetFields."

So without seeing more of the markup, I am not sure where the handler would go, but you would need to have an event similar to they are handling Loading a Record in the docs.

  • 1
    LDS isn't involved here, so it's not likely the problem. It's a good guess, though. We just need more information.
    – sfdcfox
    Apr 12 '19 at 17:01

I found a solution that seems to be working for me. Instead of initializing with the init handler, I've just set a change handler for the recordId. So:

<aura:handler name="change" value="{!v.recordId}" action="{!c.doInit}"/>

Instead of:

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

Which seems to work okay. For whatever reason, the recordId isn't populated when the init handler gets invoked. Only shortly after.

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.