1

I'm currently working on a form using lightning:RecordEditForm tag. I'm using lightning:inputField tag to display Account's BillingAddress fields (country code, state code, street, city, zip/code).

<lightning:recordEditForm objectApiName="Account" >
    <lightning:inputField aura:id="address" fieldName="BillingAddress"/>
</lightning:recordEditForm>

BillingAddress with LightningInputfield

I would like to prepopulate address fields, for instance the country field with the user's country code.

I know that when trying to get address data in my javascript controller, I have to JSON.stringify the value to display it in console.logs :

var address = component.find("address").get("v.value");
console.log(JSON.stringify(address));

I get the following results :

{"BillingCity":null,"BillingCountryCode":"US","BillingPostalCode":null,"BillingStateCode":null,"BillingStreet":null}

So I tried to set the component's value with a pre-constructed map, but no impact.

Does anyone know how I can achieve this ? Thanks !

1

Typically, I've found that if you can access an JS Object's property (in your case, BillingCity), then you can set the value like the following.

Then, the last bit where you component.set() actually (re)initializes the render cycle so that the view gets the new values you just assigned.

var addressValues = component.find("address").get("v.value");

console.log(JSON.stringify(addressValues));

addressValues["BillingCity"] = "Some City";
addressValues["BillingStateCode"] = "CA";

component.find("address").set("v.value", addressValues);
  • Hi tsalb, thanks for your answer. Indeed, I tried to set values like you showed above, but I get the following error : [Cannot set property 'BillingCity' of undefined]. Console.log displays "undefined", therefore the JS object is not defined yet – Sohaib FAROOQ Aug 10 '18 at 8:49
  • This is linked to the fact that I'm trying to prepopulate the field during doInit method. However, when I try to do it via a button, nothing happens (fields still empty) – Sohaib FAROOQ Aug 10 '18 at 9:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.