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
Sarita Pa 3Sarita Pa 3 

How to get account Id from opportunity in lightning

Can you help me in getting the information in below scenario.

I need to pass parameters to an action in lightning like opportunity id, account id. where Account Object is a look up in Opportunity. I am getting opportunity record Id but could not able to get the account Id in the controller
 
Raja babu 12Raja babu 12
you can use "force:hasRecordId " interface to get current recordId. And if you post sample of your code where you are getting stuck . This will helpfull to give solution.
Sarita Pa 3Sarita Pa 3
Thanks Raja,

Please find the code below

I am getting the record id of opportunity correctly. I am trying to get the account id which is associated with this opportunity id.by adding the code in controller like
<aura component implements="force:lightningQuickAction,force:hasRecordId">          <aura:attribute name="record" type="Object"/>    
<aura:handler name="init" value="{! this}" action="{!doInit}"/> 
</aura>

 controller: 

({ doInit: function(component,event,helper) 
    {  
                var id= component.get("v.recordId"); 
                var accid= component.get("v.account.id"); 
               alert(id+"----"+accId); 
    }
})

 
Siddhesh SinkarSiddhesh Sinkar
you can use Lightning Data Service to fetch record details , "force:hasRecordId"   enable the component to be assigned the ID of the current record but you can't access the other fields of the record . to access the other fields of record you can use lightning data service or do a  soql query on that object with passing current record Id as parameter in AuraEnabled Method which will return you data and you can use in your lightning component .  

simple way is to use Lightning Data service below is sample code which will give you the accountId of opportunity .
 
sample.cmp

<aura:component implements="force:lightningQuickAction,force:hasRecordId">
 <aura:attribute name="record" type="Object"/>
    <aura:attribute name="simpleRecord" type="Object"/>
    <aura:attribute name="recordError" type="String"/>

<force:recordData aura:id="recordLoader"
      recordId="{!v.recordId}"
      fields="AccountId"
      targetFields="{!v.simpleRecord}"
      targetError="{!v.recordError}"
      recordUpdated="{!c.handleRecordUpdated}"
      />
    
    <h1>
    {!v.simpleRecord.AccountId}
    </h1>

</aura:component>

controller:
({
    handleRecordUpdated: function (component, event) {
      var accountId = component.get("v.simpleRecord.AccountId");
      alert("accountId:::" + accountId);
    }
})

** Note :  In Javascript the values and variables are case-sensitive . 
Sarita Pa 3Sarita Pa 3
Thanks Siddesh,
I tried with the above approach and it is fetching the results only in the component. But not able to get the details in the controller.

to call the controller I am using
<aura:handler name="init" value = "{ ! this}" action="{!c.doInit}

but the results are not getting for any of the other fields.

 
Siddhesh SinkarSiddhesh Sinkar
You will not get the result in  doinit function because first the init function runs at the time of component initialization and that time Lightning Data Service(LDS)  is not loaded . After LDS is loaded it calls "handleRecordUpdated" function as per above code and you will get the data in this method only or in other function on any action but not in init. I hope if you want to use the value to set any values in controller it can be done after LDS is loaded .