I created a Lightning Component to save an account, which loads a particular account based on the URL parameter (id).
I am having a trouble with saving the record with an event, the problem is I cannot see a mechanism to call the apex method.
Could someone explain me where do I need to call my apex method? whats wrong with below code.
I ran through few samples and none of them aren't clear for me.
Thanks in advance
Please see the code below;
EVENT COMPONENT
<aura:event type="COMPONENT" description="Event template" >
<aura:attribute name="account" type="Account" />
</aura:event>
MAIN COMPONENT (register event and call to controller)
<aura:component controller="AccountsController">
<aura:registerEvent name="SaveAccount" type="c:SaveAccountEvent"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="account" type="Account" />
<div class="slds-text-heading--medium slds-p-top--x-small slds-p-bottom--x-small">Edit Account</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="name">Name</label>
<div class="slds-form-element__control">
<ui:inputText aura:id="name" value="{!v.account.Name}" class="slds-input" placeholder="..." />
</div>
</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="website">Website</label>
<div class="slds-form-element__control">
<ui:inputText aura:id="website" value="{!v.account.Website}" class="slds-input" placeholder="..." />
</div>
</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="Id">Id</label>
<div class="slds-form-element__control">
<ui:inputText aura:id="Id" value="{!v.account.Id}" class="slds-input" placeholder="..." />
</div>
</div>
<div class="slds-col slds-no-flex slds-align-bottom">
<div class="slds-button-group" role="group">
<button class="slds-button slds-button--neutral" onclick="{!c.fireSaveEvent}">Save</button>
</div>
</div>
</aura:component>
CLIENT-SIDE CONTROLLER
fireSaveEvent : function(cmp, ev) {
var appEvent = cmp.getEvent("SaveAccountDetails");
appEvent.setParams({ "account": cmp.get("v.account") });
appEvent.fire();
}
APEX METHOD
@AuraEnabled
global static Account saveAccountDetails(Account account) {
System.debug(' HASA SAVED ACCOUNT '+account);
Account updatedAccount = [ SELECT Id, Name,ShippingCity, Website
FROM Account
WHERE Id = :account.id ];
updatedAccount.Website = account.Website;
update updatedAccount;
return updatedAccount;
}
<aura:component controller="your Apex CLass Name" />
?aura:handler
for your event that will call js function from there you can call apex class function.