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
Greg FinzerGreg Finzer 

How do I get the current Account on a lightning component?

I just started using lightning today.  Sorry for the simple question.  I am creating a lightning component where I need to display some fields from the Account on the lightning page and then pass the Account.Id to a controller to get some calculated data.

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >

What goes here?

<span>{!v.Name}</span>
</aura:component>
Best Answer chosen by Greg Finzer
GovindarajGovindaraj
Hi Greg,
ApexController: accountsController
--------------
@AuraEnabled
Public class srv_getaccounts(Account accountId) {
	list<Account> lstAccount = [SELECT Name FROM Account WHERE Id =: accountId];
	return lstAccount;
}

Application: LTNG_allaccounts
------------
<aura:application extends:"force:slds" implements="force:apphostable, force:hasRecordId, force:hasSobjectName">
	<c:LTNG_allaccountsComp />
</aura:applcation>

Component: LTNG_allaccountsComp
----------
<aura:component controller="srv_getaccounts">
	<aura:attribute name="recordId" type="String" /> // Not required to add this attribute the attribute will include this in component
	<aura:attribute name="accountList" type="account []" />
	<aura:handler name="init" value"{!this}" action="{!c.doInit}" />

	<aura:iteration var="acc" items="{!v.accountList}">
		{!acc.Name}
	</aura:iteration>
</aura:component>

Controller:
-----------
doInit : function(component, event, helper) {
	helper.getaccounts(component, event, helper);
}

Helper:
-------
getaccounts : function(component, event, helper) {
	var action = component.get("c.srv_getaccounts");
		action.setParams
		({ 
			accountId : component.get("v.recordId"); //Passing parameter
		);
	action.setCallback(this,function(response) {
		var state = response.getState();
		if(state === 'SUCCESS') {
			component.set("v.accountList", response.getReturnValues());
		}
	});
	$A.EnqueueAction(action);
}
Please let us know, if this helps.

Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi Greg,
ApexController: accountsController
--------------
@AuraEnabled
Public class srv_getaccounts(Account accountId) {
	list<Account> lstAccount = [SELECT Name FROM Account WHERE Id =: accountId];
	return lstAccount;
}

Application: LTNG_allaccounts
------------
<aura:application extends:"force:slds" implements="force:apphostable, force:hasRecordId, force:hasSobjectName">
	<c:LTNG_allaccountsComp />
</aura:applcation>

Component: LTNG_allaccountsComp
----------
<aura:component controller="srv_getaccounts">
	<aura:attribute name="recordId" type="String" /> // Not required to add this attribute the attribute will include this in component
	<aura:attribute name="accountList" type="account []" />
	<aura:handler name="init" value"{!this}" action="{!c.doInit}" />

	<aura:iteration var="acc" items="{!v.accountList}">
		{!acc.Name}
	</aura:iteration>
</aura:component>

Controller:
-----------
doInit : function(component, event, helper) {
	helper.getaccounts(component, event, helper);
}

Helper:
-------
getaccounts : function(component, event, helper) {
	var action = component.get("c.srv_getaccounts");
		action.setParams
		({ 
			accountId : component.get("v.recordId"); //Passing parameter
		);
	action.setCallback(this,function(response) {
		var state = response.getState();
		if(state === 'SUCCESS') {
			component.set("v.accountList", response.getReturnValues());
		}
	});
	$A.EnqueueAction(action);
}
Please let us know, if this helps.

Thanks,
Govindaraj.S
This was selected as the best answer
Greg FinzerGreg Finzer
Govindaraj,

Thank you so much for your help.  The problem that I had was that I did not understand that you had to specifically reference the recordId.  I had it named accountId and it could not find it.  I was doing accountId : component.get("v.accountId") instead of component.get("v.recordId")

This is the final for the helper:

({
    loadAccount : function(component) {
        var action = component.get("c.loadAccount");
    
        action.setParams({
            accountId : component.get("v.recordId")
        });
    
        action.setCallback(this, function(a) {
            if (a.getState() === "SUCCESS") {
                component.set("v.account", a.getReturnValue());
            } else if (a.getState() === "ERROR") {
                $A.log("Errors", a.getError());
            }
        });
    
        $A.enqueueAction(action);
    }
})

Thanks,
Greg