0

I'm working on a lightning component and I'm trying to retrieve lead record details through force:recorddata but actually it's returning null results.

My component:

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="leadRecord" type="Object"/>        
<aura:handler name="init" value="{!this}" action="{!c.dohitme}"/>
   <force:recordData aura:id="recordLoader"
    recordId="{!v.recordId}"
    fields="status"
    targetFields="{!v.leadRecord}"
    targetError="{!v.recordLoadError}"
    />
</aura:component> 

My Controller:

({
    dohitme : function(component, event, helper) {
        var userId = $A.get("$SObjectType.CurrentUser.Id");
        console.log(userId);
        console.log(component.get("v.recordId"));
        var speedtolead = component.get("v.leadRecord").Status;
        console.log(speedtolead);
    }
})

Did I miss something?

enter image description here

1 Answer 1

1

From the documentation, you can see they note the following:

force:recordData loads data asynchronously since it may retrieve data from the server. To track when the record is loaded or changed, use the recordUpdated event or place a change handler on targetRecords or targetFields.

Since it's async, you won't have any value in the initialization as you're attempting to do. As it mentioned, you can leverage the recordUpdated event

The event fired when the record is loaded, updated, or deleted.

This will ensure it's loaded before you attempt to do anything within the record. If it does fail to load, you can check your recordLoadError attribute (which you used in recordData, but need to create the attribute)

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="leadRecord" type="Object"/>        
<aura:attribute name="recordLoadError" <type="String"/> <!-- store error -->
   <force:recordData aura:id="recordLoader"
    recordId="{!v.recordId}"
    fields="Status"
    targetFields="{!v.leadRecord}"
    targetError="{!v.recordLoadError}"
    recordUpdated="{!c.recordUpdated}" <!-- put logic on load -->
    />
</aura:component> 
({
    recordUpdated : function(component, event, helper) {
        var changeType = event.getParams().changeType;
        if (changeType === "LOADED") {
            var userId = $A.get("$SObjectType.CurrentUser.Id");
            var speedtolead = component.get("v.leadRecord").Status;
        } else if (changeType === "ERROR") {
            ...
        } /* also have REMOVED and CHANGED */
        
    }
})
7
  • Thank you for your answer do you want me to create a separate action named recordUpdated in controller
    – Pavan tej
    Commented Jul 16, 2021 at 14:52
  • 1
    if that's all your current action does (dohitme), you can just rename it and remove the aura:handler for init as you won't need it. You want to handle the recordUpdated event. Commented Jul 16, 2021 at 15:20
  • Sorry, I'm new to lightning actually change type is returning ERROR only. How to debug the scenario
    – Pavan tej
    Commented Jul 16, 2021 at 15:54
  • 1
    Try capitalizing the Status field (as that's its actual API name) and making sure your custom field is exactly the same as the API name as well: fields="Status,Speed_to_Dial_main__c" Commented Jul 16, 2021 at 15:59
  • Same issue after we have updated
    – Pavan tej
    Commented Jul 16, 2021 at 16:42

You must log in to answer this question.

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