function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gaston LeferGaston Lefer 

aura attribute dynamic default value

How do you set a dynamic aura attribute default value like current userinfo instead of hardcoding. See below.

The harcoding accountid works, not dynamic contactid/not supported.
 
<aura:attribute name="lookupcase" type="Case" default="{
		'sobjectType': 'Case',
        'ContactId': '{!v.userInfo.ContactId}',                                                
        'AccountId': '0012D000002oAkt'  
                                                         
	}"/>

 
Best Answer chosen by Gaston Lefer
sfdcMonkey.comsfdcMonkey.com
hi gaston, we can not set attribute default value as dynamic, you can achive this by set the attribute value by javaScript controller on component load for example :
 
<aura:component>

<aura:handler name="init" value="this" action="{!c.doInit}"/>
<aura:attribute name="lookupcase" type="Case" default="{
		'sobjectType': 'Case',
                'ContactId': '',                                                
                'AccountId': '0012D000002oAkt'  
                                                         
	}"/>

</aura:component>
javaScript controller :
({
	doInit : function(component, event, helper) {
	var action = component.get("c.fetchUser");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
               // set current user information on userInfo attribute
                component.set("v.userInfo", storeResponse);

               // @@@@@ set default value here @@@@
                var getContactId = component.get("v.userInfo.ContactId");
                component.set("v.lookupcase.ContactId" , getContactId ) ;
            }
        });
        $A.enqueueAction(action);
    }
})

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks 
http://sfdcmonkey.com  (http://sfdcmonkey.com )
 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi gaston, we can not set attribute default value as dynamic, you can achive this by set the attribute value by javaScript controller on component load for example :
 
<aura:component>

<aura:handler name="init" value="this" action="{!c.doInit}"/>
<aura:attribute name="lookupcase" type="Case" default="{
		'sobjectType': 'Case',
                'ContactId': '',                                                
                'AccountId': '0012D000002oAkt'  
                                                         
	}"/>

</aura:component>
javaScript controller :
({
	doInit : function(component, event, helper) {
	var action = component.get("c.fetchUser");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
               // set current user information on userInfo attribute
                component.set("v.userInfo", storeResponse);

               // @@@@@ set default value here @@@@
                var getContactId = component.get("v.userInfo.ContactId");
                component.set("v.lookupcase.ContactId" , getContactId ) ;
            }
        });
        $A.enqueueAction(action);
    }
})

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks 
http://sfdcmonkey.com  (http://sfdcmonkey.com )
 
This was selected as the best answer
Gaston LeferGaston Lefer
why did you set 'ContactId': ''  , empty? doesnt work  
sfdcMonkey.comsfdcMonkey.com
beacuse we are setting contact id in doInit function, and above code working fine in my dev org
Gaston LeferGaston Lefer
below Ivve the code below, not sure why it is empty and doesnt show it current contact id
<force:inputField value="{!v.lookupcase.ContactId}" class="slds-input"/>
Gaston LeferGaston Lefer
You were right it work with ui:input, for some reason doesnt work with force:input, adding aua:id is the way to go?
var action = component.get("c.fetchUser");
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var storeResponse = response.getReturnValue();
           // set current user information on userInfo attribute
            component.set("v.userInfo", storeResponse);

           // @@@@@ set default value here @@@@
            var getContactId = component.get("v.userInfo.ContactId");
            component.set("v.lookupcase.ContactId" , getContactId ) ;
            component.find("contactlookup").set("v.lookupcase.ContactId" , getContactId);

        }
    });
    $A.enqueueAction(action);
 
<force:inputField value="{!v.lookupcase.ContactId}" class="slds-input" aura:id="contactlookup"/>

 
Tanuj TyagiTanuj Tyagi
Hi Piyush,
I am using lookup field for Owner Id for my custom Object Visit__c. NOT able to set a value on DOINIT Method

<aura:attribute name="Visits" type="Visit__c" default="{'sobjectType':'Visit__c','OwnerId':'','Owner':{'sobjectType':'','Name':''}}"/>


<force:inputField aura:id="visitTeamlookup" value="{!v.Visits.OwnerId}"/>
 

Tried all these below solutions :
        component.find("visitTeamlookup").set("v.Visits.OwnerId" , '0051N000005InMFQA0');
        component.set("v.Visits.Owner.sobjectType" , 'User');
        component.set("v.Visits.Owner.Name" , 'Development Admin');
        component.set("v.Visits.OwnerId" , '0051N000005InMFQA0' ) ;

Nothing works.