0

I need to get the initial inputfield value from the database before user make any changes.

<aura:handler name="render" value="{!this}" action="{!c.onRender}"/>
<lightning:recordEditForm recordId="{!v.recordId}" objectApiName="myObj__c">
    <lightning:inputField aura:id="myField" fieldName="myField__c" onchange="{!c.myChangeEvent}"/>
</lightning:recordEditForm>

On javascript I have this:

onRender: function(component, event, helper) {
    var myFieldValue= component.find("myField").get("v.value");
    alert(myFieldValue);  //undefined
},

So this doesn't work and give me undfined, however when the user change the actual value and the onchange event fire, it does work:

myChangeEvent: function(component, event, helper) {
    var myFieldValue= component.find("myField").get("v.value");
    alert(myFieldValue);  // works
},

Cleary the difference is that the user explicitly set the value so component.find("myField").get("v.value") was able to pick it up, but how do I get the value already saved in database in the sObject?

1 Answer 1

0

You can't get the value on render, because the record hasn't been loaded yet. I would recommend using force:recordData and storing the value you get when recordUpdated fires. You can read more about that in this answer. Because of how Lightning Data Service works, both your edit form and force:recordData should result in only a single server call, so this is a great way to get the values before the user makes any edit. Alternatively, you'll be able to get the original values up until the time you submit the form to save the record.

This site is temporarily in read-only mode and not accepting new answers.

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