+ Start a Discussion
sawaji varunsawaji varun 

How to Update Accounts records using lightning?

AccountsController.apxc
public class AccountsController {
    @AuraEnabled public static Account          getAccount(){
        return (Account) Database.query( ' SELECT Name,Type,NumberOfEmployees,TickerSymbol,Phone FROM Account  ' )[0];
    }
    @AuraEnabled public static Account          saveAccount(Account account){
        upsert account;
        return account;
    } 
}

AccountsListController.js
({
    init : function(component, event, helper) {
        console.log('init');
        var action2 = component.get("c.getAccount");        
        action2.setCallback(this, function(response) {
            console.log(response.getReturnValue());
            component.set("v.account", response.getReturnValue());
        });
        $A.enqueueAction(action2);
    }, 
    save : function(component, event, helper) {
        console.log('save:1');
        var action = component.get("c.saveAccount");
        var account = component.get("v.account");
        action.setParams({"account": account});
        action.setCallback(this, function() {  console.log('SAVED.');  } );
        $A.enqueueAction(action);
        console.log('save:end');
    },
})

({
   //Fetch the accounts from the Apex controller
  getAccountList: function(component) {
    var action = component.get("c.getAccounts");

    //Set up the callback
    var self = this;
    action.setCallback(this, function(actionResult) {
        component.set("v.accounts", actionResult.getReturnValue());            
    });
    $A.enqueueAction(action);
  }   
})

AccountList.cmp
<aura:component controller="AccountsController" implements="forceCommunity:availableForAllPageTypes">
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
    <aura:attribute name="account"  type="Account" />
    <h1>Test</h1>
    <form>
         
        <ui:inputText aura:id="client"
            label="{!$ObjectType.Account.fields.Name.Label}"
            class="form-control"
            value="{!v.account.Name}"
            placeholder="Name"
        />
        <ui:inputText aura:id="client"
            label="{!$ObjectType.Account.fields.Type.Label}"
            class="form-control"
            value="{!v.account.Type}"
            placeholder="Type"
        />
        <ui:inputText aura:id="client"
            label="{!$ObjectType.Account.fields.NumberOfEmployees.Label}"
            class="form-control"
            value="{!v.account.NumberOfEmployees}"
            placeholder="NumberOfEmployees"
        />
        <ui:inputText aura:id="client"
            label="{!$ObjectType.Account.fields.TickerSymbol.Label}"
            class="form-control"
            value="{!v.account.TickerSymbol}"
            placeholder="TickerSymbol"
        />
        <ui:inputText aura:id="client"
            label="{!$ObjectType.Account.fields.Phone.Label}"
            class="form-control"
            value="{!v.account.Phone}"
            placeholder="Phone"
        />
        <br />
        <ui:button class="form-control" aura:id="button" label="Save" press="{!c.save}"/>
        <br />
    <br />
       
    </form>
</aura:component>
 
Amit GoyalAmit Goyal
Hi sawaji varun,

I could share the code with you but I guess you are missing the code for the helper file at the time of upsert account, please review the first application [ExpenseTracker] shared in "Lightning Component Developer's Guide", that will brush up the logic.

Let me know if anything else you need to ask.

Thanks,
Amit Goyal
sawaji varunsawaji varun
Thanks for reply, I will check now.