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
Tony RissoneTony Rissone 

Aura attribute, set default value based on record data

Basically i have an attribute in an aura component that i want to be dynamic based on the record the component in embedded in.
 
<aura:attribute name="user" type="String" default=the ownerId/>


Now i know how to get record data into a aura component 'force:recordData'. Would it be like:
 
<aura:attribute name="user" type="String" default= {!v.accountRecord.OwnerId}/>

<force:recordData aura:id="recordLoader"
    recordId="{!v.recordId}"
    fields="OwnerId"
    targetFields="{!v.accountRecord}"
    targetError="{!v.recordLoadError}"
    />




 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Tony,

You need to store it in an attribute of type object and then use this attribute to get these values.

For example SimpleRecord attribute of type object and then we are showing the specific values Name, BillingCity, BillingState in lightning:formattedText using v.simpleRecord.BillingState, v.simpleRecord.BillingCity, v.simpleRecord.Name
 
<aura:component implements="flexipage:availableForRecordHome, force:hasRecordId"> <!--inherit recordId attribute-->
<aura:attribute name="record" type="Object" />
<aura:attribute name="simpleRecord" type="Object" />
<aura:attribute name="recordError" type="String" />
<force:recordData aura:id="recordEditor"
    fields="Name,BillingCity,BillingState"
    recordId="{!v.recordId}"
    targetError="{!v.recordError}"
    targetRecord="{!v.record}"
    targetFields ="{!v.simpleRecord}"
    mode="EDIT" />
    <!-- Display a lightning card with details about the record -->
    <lightning:card iconName="standard:account" title="{!v.simpleRecord.Name}" >
        <div class="slds-p-horizontal--small">
            <p class="slds-text-heading--small">
                <lightning:formattedText title="Billing State" value="{!v.simpleRecord.BillingState}" /></p>
            <p class="slds-text-heading--small">
                 <lightning:formattedText title="Billing City" value="{!v.simpleRecord.BillingCity}" /></p>
        </div>
    </lightning:card>
    <br/>
    <!-- Display an editing form -->
    <lightning:card iconName="action:edit" title="Edit Account">
        <div class="slds-p-horizontal--small">
            <lightning:input label="Account Name" value="{!v.simpleRecord.Name}"/>
            <br/>
            <lightning:button label="Save Account" variant="brand" onclick="{!c.handleSaveRecord}" />
        </div>
    </lightning:card>
    <!-- Display Lightning Data Service errors, if any -->
    <aura:if isTrue="{!not(empty(v.recordError))}">
        <div class="recordError">
            {!v.recordError}
        </div>
    </aura:if>
</aura:component>

I don't think you would be able to assign the values directly as mentioned in the above example and also on https://trailhead.salesforce.com/en/content/learn/v/modules/lightning_data_service/lightning_data_service_manipulate_records

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.