[SalesForce] window.onload equivalent for aura components

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");
    console.log(caseId);
    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

Best Answer

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.

Related Topic