Search code examples
salesforce-lightning

Unable to set value dynamically for Lightning:InputField


I have created a lightning component with lightning record edit form and a couple of fields. Wrote a Javascript method such that When I change the first field to a particular value then second field is updated. Below is my code.

<aura:component implements="force:appHostable">
   <aura:attribute name='test' type='String'/>
   <lightning:recordEditForm 
                          aura:id="editForm"
                          objectApiName="Case"
                          recordTypeId="{XXXXXXXXXXXXX}">
    <div class="slds-col slds-size_1-of-2 slds-p-around_x-small">
        <lightning:inputField aura:id="subject" fieldName="Subject" onchange="{!c.subjectChanged}"/>
    </div>
    <div class="slds-col slds-size_1-of-2 slds-p-around_x-small">
        <lightning:inputField aura:id="description" value="{!v.test}" fieldName="Description"/>
    </div>
</lightning:recordEditForm>
</aura:component>

Below is the Controller code

({
    subjectChanged : function(component, event, helper) {
        var subjectValue = component.find("subject").get("v.value");
        if(subjectValue === "U") {
            //var descriptionValue = component.find("description");
            //descriptionValue.set("v.value","User");
            component.set('v.test','User');
        }

    }
})
  1. Updated Subject field to U, then Description got updated to 'User'
  2. Update the Description field value to XYZ or clear the value.
  3. Change the subject field to X and then update back to U.
  4. Notice that JS method got executed, but description is not updated to 'User'.

Whenever, user updates subject field to U, I want the description to be set to User. Any thoughts on this?


Solution

  • From documentation: https://developer.salesforce.com/docs/component-library/bundle/lightning:inputField/documentation

    .....The assumption is that there are unsaved changes that should not be overwritten. If you want to be able to overwrite user changes, you can use lightning:input instead....