1

I have 2 attributes, "account" and "newContact", I want to set the default value for Account.Name for "newContact" as the value of "account" attribute, i.e. whatever will be the value of account attribute that must be assigned to Account.Name in "newContact" attribute. Can someone please help !

<aura:attribute name="account" type="String" />
<aura:attribute name="newContact" type="Contact" 
                default="{'sObjectType':'Contact', 'FirstName':'', 
                           'LastName':'', 'Account.Name':'', 
                           'Birthdate':''}"/>

Thanks in advance !!

  • 1
    what you have write so far? Please Share it. – am_Aravi Jan 8 '18 at 8:59
  • In your component component and helper you can perform get or set any attribute. Where exactly are you stuck? – Raul Jan 8 '18 at 9:14
  • My parent component is setting the value for "account" attribute and on the basis of this value I want to set AccountName for my "newContact" attribute. I am not able to use "{!v.account}" inside default. – Ashna Malhotra Jan 8 '18 at 9:23
  • Here the type should be sobjectType not sObjectType it's case sensitive; – itzmukeshy7 Jan 8 '18 at 10:35
2

The Mistake you made in the Attribute Decleration of "newContact" attribute.You cannot set the the default value of Contact of Account.Name directly as a String.You should set the Account of Contact as an Object and then you can set the "account" attribute value for "newContact" like this.

Component:

<aura:attribute name="account" type="string" default="abc"/>
<aura:attribute name="newContact" type="Contact" 
            default="{'sObjectType':'Contact', 'FirstName':'', 
                       'LastName':'', 'Account':{Id:'',Name:''}, 
                       'Birthdate':''}"/>

<aura:handler name="init" value="{!this}" action="{!c.init}"/> 

Controller:

({
    init: function(component,event,helper){

        var contact = component.get('v.newContact');
        contact.Account.Name = component.get('v.account');
        component.set('v.contact', contact);
        console.log(component.get('v.newContact'));
    }
)}

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.