0

I develop a lightning component where I use a object as an attribute.

In my lightning controller, I would like to set a value on that object but it doesn't work, nothing happened..

This is my code:

View:

<aura:attribute name="account" type="Object" />
<lightning:formattedText valule="{!v.account.Name}" />

Lightning controller:

let account = component.get("v.account", true);
account.Name = "Test";
component.set("v.account", account);

Can you help me please? I think it doesn't matter but I develop in a community.

  • please edit/format your code =) – glls Feb 20 '19 at 16:51
2

Couple of issues:

  1. Your object account is not initialized and if you are not doing it in init, then you will receive error. If you are not initializing it in JS, then use the format as mentioned in the other answer by @sfdcfox

    <aura:attribute name="account" type="Account" default="{sobjectType:'Account'}" />
    

    If you want to use the declaration as is, then you need to make sure the JS code written is in init and that you initialize the account there as:

    let account = {"sobjectType": "Account", "Name": "Test"};
    component.set("v.account", account);
    
  2. The syntax for component.get is not correct, it only expects a key to get the value. It should be written as below. Again this is assuming that account is already initialized on the component.

    let account = component.get("v.account");
    
  3. Unless it's a typo, the attribute name should be value here:

    <lightning:formattedText value="{!v.account.Name}" />
    
| improve this answer | |
  • It works, thank you. I have a last question, how can I get my account with the javascript controller? At the end of the page when the user submit a form, I would like to get my account to do treatment but the result is undefined. I don't understand because in my lightning component, I see values for fields. Doing component.get("v.account") ? How can I format my code? – Aurélien Laval Feb 21 '19 at 10:06
  • Yes - you can retrieve the value of the attribute usingcomponent.get("v.account"). – Jayant Das Feb 21 '19 at 13:34
1

Object should not be used for objects (complicated, I know). Select the correct type of object as the type. Also make sure to initialize it, or you'll get an error.

<aura:attribute name="account" type="Account" default="{sobjectType:'Account'}" />
| improve this answer | |
  • Ok thank you but it doesn't work, the page doesn't load itself. – Aurélien Laval Feb 20 '19 at 17:01

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.