Create Account Record Using Lightning Component

Introduction:

A Lightning Component is an UserInterface framework which is used for the development of dynamic web applications which can be used in mobile Phone and our desktops. For this purpose, JavaScript is used on the client-side and Apex is used on the server-side. The Lightning Component framework is built on the open-source Aura framework. The Aura framework enables us to build apps that are not dependent on our data in Salesforce.

Please Fallow Below Step To Create Account Record Using Lightning Component.

STEP 1:- Create Apex Class For Creating Account Record.

public class InsertAccount {
    @AuraEnabled
    public static Account createAccount (Account acc) {
        
        upsert acc;
        return acc;
    }

}

STEP 2:- Create Lightning Component.

<aura:component controller="InsertAccount" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="newAccount" type="Account"  default="{ 'sobjectType': 'Account',
                                                               'Name': '',
                                                               'Phone': ''
                                                               }"/>
    <div class="row">
        <form>
            <ui:inputText aura:id="AccountName" label="New Account Name"
                          class="slds-input"
                          labelClass="slds-form-element__label"
                          value="{!v.newAccount.Name}"
                          required="true"/>
            <ui:inputText aura:id="AccountName" label="Phone Number"
                          class="slds-input"
                          labelClass="slds-form-element__label"
                          value="{!v.newAccount.Phone}"
                          required="true"/><br/>
            
            <lightning:button variant="brand" label="Create Account" title="Create Account" onclick="{!c.createAccount}"/>
            
            
        </form>
    </div>
</aura:component >

STEP 3:- Create Clint Side Controller.

({
  createAccount : function(component, event) {
    debugger;
    var newAcc = component.get("v.newAccount");
    var action = component.get("c.createAccount ");
    action.setParams({ 
        "acc": newAcc
    });
    action.setCallback(this, function(response) {
           var state = response.getState();
            if (state === "SUCCESS") {
                var name = response.getReturnValue();
               alert("Account Inserted Your Name is" + name);
            }
        });
    $A.enqueueAction(action)
}
})

STEP 4:- Create Lightning Application To Test.

<aura:application extends=”force:slds” >

<c:componentname/>

</aura:application>



Post a Comment

Post a Comment (0)

Previous Post Next Post