Viewing a Record using Lightning Data Service force:recordData

Viewing a Record using Lightning Data Service force:recordData

Component:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:attribute name="record" type="Object" description="The record object" />
    <aura:attribute name="recordError" type="String" description="An error message bound to force:recordData" />
    <lightning:card>
        <force:recordData aura:id="recordLoader"
                          recordId="{!v.recordId}"
                          fields="Subject, Description"
                          targetFields="{!v.record}"
                          targetError="{!v.recordError}"
                          recordUpdated="{!c.handleRecordUpdated}"
                          mode="VIEW"/>
        Case Id: {!v.recordId}<br/>
        Case Subject: {!v.record.Subject}
    </lightning:card>
</aura:component>

JavaScript:

({
    handleRecordUpdated : function(component, event, helper) {
        var eventParams = event.getParams();
        if(eventParams.changeType === "LOADED") {
            console.log( 'Record Loaded' );
            var flow = component.find("flowData");
        } else if(eventParams.changeType === "CHANGED") {
            console.log( 'Record Changed' );
        } else if(eventParams.changeType === "REMOVED") {
            console.log( 'Record Removed' );
        } else if(eventParams.changeType === "ERROR") {
            console.log("Error Change Type");
        }
    }
})

Output:

Note:

force:recordData loads the record asynchronously by pulling the data from the Salesforce Server. So, recordUpdated should be used to monitor when the record is Loaded, Changed(Updated), Removed(Delete) and errored out.

Leave a Reply