4

I have a number of lightning:inputField that i'm setting based on certain criteria, these all work except for picklist field types. The code below shows how i am setting the fields. The country field is the picklist and the 2 alerts have the correct value in them, however the value is not getting set on country. Is there a different method for setting picklist fields?

var address = res.getReturnValue();
var country = component.find('Country__c');
var pcodeFld = component.find('Postcode__c');

if(country != undefined){
 alert(country);
 alert(address.Country);
 country.set('v.value', address.Country);
}
if(pcodeFld != undefined){
 pcodeFld.set('v.value', address.Postcode);
}

EDIT: I can set a default value whilst using $A.createComponents using .set("v.value", "United Kingdom");

it just seems that i can't set it after the component has rendered based on some user action.

Reproduceable Example:

I just can't seem to set the value of any picklist fields when using lightning:inputField.

Component

<aura:component implements="flexipage:availableForRecordHome, force:hasRecordId">
<div class="slds-p-bottom_large slds-p-left_large" style="width:500px">
    <lightning:recordEditForm aura:id="recordViewForm" 
                                 recordId="{!v.recordId}"
                                 objectApiName="Contact">
        <lightning:messages />
        <lightning:inputField fieldName="FirstName" />
        <lightning:inputField fieldName="LastName" aura:id="lastname"/>
        <lightning:inputField fieldName="Birthdate" />
        <lightning:inputField fieldName="Phone" />
        <!--Picklist-->
        <lightning:inputField fieldName="Country__c" aura:id="Country__c"/>
        <lightning:button label="Update" variant="brand" onclick="{!c.update}"/>
    </lightning:recordEditForm>
</div>    
</aura:component>

Controller

({    
    update: function(component, event, helper) {
        //alert(component.find("Country__c"));
       component.find("Country__c").set("v.value", "Ireland");
    },

})
  • Can you add markup as well, thanks. and is that lightning:input or lightning:inputField? – Pranay Jaiswal May 30 '18 at 16:28
  • the markup is dynamically generated using $A.createComponents and theres a lot of it so its difficult to add here – paul May 30 '18 at 16:32
  • 1
    does same code work if component is not dynamically ? There are few issues with setter when components are created using $A.createComponents. salesforce.stackexchange.com/questions/196382/… – Pranay Jaiswal May 30 '18 at 16:35
  • No, I just tried with a non-dynamic lightning:inputfield and the value still doesnt get set – paul May 30 '18 at 16:43
  • is your address.Country is in below format? var opts = new Array(); opts.push({label: results.Name, value: results.Name}); component.set("v.options", opts); If not then give label and Value in your Country – salesforce Developer May 30 '18 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.