[SalesForce] Set dynamic value for attribute’s default value in lighnting

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':'', 

Thanks in advance !!

Best Answer

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.


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

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


    init: function(component,event,helper){

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