+ Start a Discussion
sheila srivatsavsheila srivatsav 

fetch LDS attribute in controller.js

I have a lightning data service .

I have shown the complete code I have with me.

<aura:attribute name="simpleRecord" type="Object" description="A simplified view record object to be displayed"/>
    <aura:attribute name="recordError" type="String" description="An error message"/>
 <aura:attribute name="record" type="Object" description=""/>

    <force:recordData aura:id="somecaseRecord"
                          recordId="{!v.caseId}"
                          targetError="{!v.recordError}"
                          targetRecord="{!v.record}"
                          targetFields="{!v.simpleRecord}"
                          mode="VIEW"                      
                          fields="Id,Status"/>

I have Status is a field defined in the case object and case Id is generated during the init handler.

I want to fetch the value of the status field in the controller.js

if (component.get("v.simpleRecord.Status") != "Closed")
{
 //then do some logic
}

I tried doing like this also.
if (component.get("v.record").Status  != "Closed")
{
 //then some logic I will do
}

But its not fetching the status field value 

Let me know where I am going wrong.

thanks
sheila S
Raj VakatiRaj Vakati
Use this code

 
<aura:attribute name="simpleRecord" type="Object" description="A simplified view record object to be displayed"/>
    <aura:attribute name="recordError" type="String" description="An error message"/>
 <aura:attribute name="record" type="Object" description=""/>

    <force:recordData aura:id="somecaseRecord"
                          recordId="{!v.caseId}"
                          targetError="{!v.recordError}"
                          targetRecord="{!v.record}"   recordUpdated="{!c.handleRecordUpdated}"

                          targetFields="{!v.simpleRecord}"
                          mode="VIEW"                      
                          fields="Id,Status"/>
 
handleRecordUpdated: function(component, event, helper) {
    var eventParams = event.getParams();
    if(eventParams.changeType === "CHANGED") {
        // get the fields that changed for this record
        var changedFields = eventParams.changedFields;
        console.log('Fields that are changed: ' + JSON.stringify(changedFields));
        // record is changed, so refresh the component (or other component logic)
        var resultsToast = $A.get("e.force:showToast");
        resultsToast.setParams({
            "title": "Saved",
            "message": "The record was updated."
        });
        resultsToast.fire();

    } else if(eventParams.changeType === "LOADED") {
	
	var stst =component.get("v.record").Status
	
	
        console.log("account loaded:::::" + component.get("v.simpleRecord").status);
        // record is loaded in the cache
    } else if(eventParams.changeType === "REMOVED") {
        // record is deleted and removed from the cache
    } else if(eventParams.changeType === "ERROR") {
        // there’s an error while loading, saving or deleting the record
    }
}

 
Raj VakatiRaj Vakati
Its standard way to get the component fired events 

event.getParams() --> retuns Object and evenrs are fired by SF component