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
Lek LextechLek Lextech 

How can I get other fields' value of recordId attribute in Lightning component?

Hi all,

How can I get other fields' value of recordId attribute in Lightning component?
For instance, recordId of Opportunity, want to get AccountId of this opportunity
Best Answer chosen by Lek Lextech
Dev_AryaDev_Arya
Hi Lex, are you using recordData, you can directly access the fields in the lightning component.
For example for opportunity, you can directly access Opportunity Account ID. A very simple example.:
<!--
 - Created by darya on 11/29/2017.
 -->

<aura:component description="OpportunityTestComponent" implements="flexipage:availableForRecordHome,force:hasRecordId">

    <aura:attribute name="record" type="Object"
                    description="The record object to be displayed"/>
    <aura:attribute name="OpptyRecord" type="Object"
                    description="A simplified view record object to be displayed"/>
    <aura:attribute name="recordError" type="String"
                    description="An error message bound to force:recordData"/>

    <force:recordData aura:id="record"
                      layoutType="FULL"
                      recordId="{!v.recordId}"
                      targetError="{!v.recordError}"
                      targetRecord="{!v.record}"
                      targetFields="{!v.OpptyRecord }"
                      mode="VIEW"/>

    <!-- Display a lightning card with details about the record -->
    <div class="Oppty Details">
        <lightning:card iconName="lightning:card" title="{!v.OpptyRecord.Name}" >
            <div class="slds-p-horizontal--small">
                <p class="slds-text-heading--small">
                    <lightning:formattedText title="Amount" value="{!v.OpptyRecord.Amount}" /></p>
// Account opportunity Account ID
                <p class="slds-text-heading--small">
                    <lightning:formattedText title="Account Id" value="{!v.OpptyRecord.AccountId }" /></p>
                <p class="slds-text-heading--small">
                    <lightning:formattedPhone title="Stage" value="{!v.OpptyRecord.StageName }" /></p>
            </div>
        </lightning:card>
    </div>

    <!-- Display Lightning Data Service errors, if any -->
    <aura:if isTrue="{!not(empty(v.recordError))}">
        <div class="recordError">
                {!v.recordError}</div>
    </aura:if>


</aura:component>

No Apex needed. If you need more insight, feel free to ask.

Cheers, Dev
 

All Answers

Suraj TripathiSuraj Tripathi
Hi  Lek Lextech, 

Please try this code :

In Component use force: hasRecordId :​
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes, force:hasRecordId,force:hasSObjectName" access="global">

Helper Code : 
helper : function(c, e, h) {

        var action = c.get("c.getOppList"); 

        action.setParams({

            "oppid": c.get("v.recordId")                     

        });

Apex Class : 
 
@AuraEnabled

    public static Opportunity getOppList(String oppid) {

     Opportunity opp = [Select Id, AccountId FROM Opportunity Where Id =: oppid limit 1];

Hope it Helps you. Please mark this as solved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Regards ,
Suraj
Lek LextechLek Lextech
Thank Suraj, can we solve this without using APEX? Regards, Narongrit Inwang (Lek) +66891826386
Suraj TripathiSuraj Tripathi
Hi  Lek Lextech, 

In order to get the AccountId and its data from opportunity Id, you need to write a SOQL query for that.

Regards ,
Suraj
Dev_AryaDev_Arya
Hi Lex, are you using recordData, you can directly access the fields in the lightning component.
For example for opportunity, you can directly access Opportunity Account ID. A very simple example.:
<!--
 - Created by darya on 11/29/2017.
 -->

<aura:component description="OpportunityTestComponent" implements="flexipage:availableForRecordHome,force:hasRecordId">

    <aura:attribute name="record" type="Object"
                    description="The record object to be displayed"/>
    <aura:attribute name="OpptyRecord" type="Object"
                    description="A simplified view record object to be displayed"/>
    <aura:attribute name="recordError" type="String"
                    description="An error message bound to force:recordData"/>

    <force:recordData aura:id="record"
                      layoutType="FULL"
                      recordId="{!v.recordId}"
                      targetError="{!v.recordError}"
                      targetRecord="{!v.record}"
                      targetFields="{!v.OpptyRecord }"
                      mode="VIEW"/>

    <!-- Display a lightning card with details about the record -->
    <div class="Oppty Details">
        <lightning:card iconName="lightning:card" title="{!v.OpptyRecord.Name}" >
            <div class="slds-p-horizontal--small">
                <p class="slds-text-heading--small">
                    <lightning:formattedText title="Amount" value="{!v.OpptyRecord.Amount}" /></p>
// Account opportunity Account ID
                <p class="slds-text-heading--small">
                    <lightning:formattedText title="Account Id" value="{!v.OpptyRecord.AccountId }" /></p>
                <p class="slds-text-heading--small">
                    <lightning:formattedPhone title="Stage" value="{!v.OpptyRecord.StageName }" /></p>
            </div>
        </lightning:card>
    </div>

    <!-- Display Lightning Data Service errors, if any -->
    <aura:if isTrue="{!not(empty(v.recordError))}">
        <div class="recordError">
                {!v.recordError}</div>
    </aura:if>


</aura:component>

No Apex needed. If you need more insight, feel free to ask.

Cheers, Dev
 
This was selected as the best answer
Lek LextechLek Lextech
Thank you both of you. I can use both ways to solve my problem
Lek LextechLek Lextech
One more question Arya, Can we iterate {!v.OpptyRecord } by using aura:iteration?
Dev_AryaDev_Arya
Hi Lex,

I am sorry I didnt get what you mean by iterating {!v.OpttyRecord}. Could you elaborate your use case. JFYI, OpptyRecord is one object entity here, not a list.
 
Rajeevan SRajeevan S
Hi Dev_Arya,

How can get Selected fields of Account Object into a Lightning Component. I have to use that component to add it to Account record detail page as a new tab.?

Give me a small example for this. I want to get field values and keep in a component.
Jatin Sethi 26Jatin Sethi 26
Hi Dev_Arya,

Can we get the value of account in helper function as well???