Account Records Using Lightning Component

Create an Account Records Using Lightning Component


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.

Follow the steps below to create an Account Records Using Salesforce Lightning Component:

Step 1: Create a Lightning Component

Firstly we have to create a Lightning Component and for that go to setup->Developer console, At Developer, console Click on New to create a lightning component and use the code mentioned below:

<aura:component controller=”createAccountRecords”>
    <aura:attribute name=”createAcc” type=”Account” default=”{‘sObjectType’ : ‘Account’,’Name’ : ”,’Rating’ : ”}”/>
    <aura:attribute name=”objName” type=”String” default=”Account”/>
    <aura:attribute name=”fldName” type=”String” default=”Rating”/>
    <aura:attribute name=”ratingList” type=”List”/>
    <aura:handler name=”init” value=”{!this}” action=”{!c.doInit}”/>
    <div class=”slds-p-around_small”>
        <lightning:input type=”Text” label=”Name” value=”{!v.createAcc.Name}”/>
        <lightning:input type=”Text” label=”Account Number” value=”{!v.createAcc.AccountNumber}”/>
        <lightning:input type=”Email” name=”email2″ value=”{!v.createAcc.Email}” label=”Email ID”/>
        <lightning:input type=”Phone” label=”Phone Number” value=”{!v.createAcc.Phone}”/>
        <lightning:select label=”Rating” value=”{!v.createAcc.Rating}”>
            <option value=””>—None—</option>
            <aura:iteration items=”{!v.ratingList}” var=”ac”>
                <option value=”{!ac}”>{!ac}</option>
        <lightning:button label=”Save” iconPosition=”left” variant=”brand” onclick=”{!c.doSave}”/>
        <lightning:button label=”Cancel” iconPosition=”right” variant=”destructive” onclick=”{!c.docancel}”/>

Step 2: Create a Lightning Controller for the component

Now, we have created a component, firstly we have to handle the logic at the lightning level then we can move to apex side, for that we have to create a component controller by clicking on the controller in the panel in the right side and use the code mentioned below:

    doInit : function(component, event, helper) {
        var action = component.get(‘c.getPickList’);
            objName : component.get(‘v.objName’),
            fldName : component.get(‘v.fldName’)
            var resultValue = result.getReturnValue();
    doSave : function(component, event, helper) {
        var action = component.get(‘c.createAccount’);
            ac : component.get(‘v.createAcc’)
            var getAllValue = component.get(‘v.createAcc’);
    docancel : function(component, event, helper) {

Step 3: Create an Apex class and define the method to create the record

Now the component which we have created will call lightning controller and then the controller will invoke Method that is written in apex class for that use the code mentioned below:

public class createAccountRecords {
    public static List<String> getPickList(String objName,String fldName) {
        List<String> pkList = new List<String>();Map<String,Schema.SObjectType> allObj = Schema.getGlobalDescribe();
        Map<String,Schema.SObjectField> allFlds = allObj.get(objName).getDescribe().fields.getMap();
        List<Schema.PicklistEntry> pickList = allFlds.get(fldName).getDescribe().getPickListValues();
        for(Schema.PicklistEntry pk : pickList){
        return pkList;
    public static Account createAccount(Account ac){
        insert ac;
        return ac;

Step 4: Create a Lightning application which will host the component in the App

Lightning App has to created for the hosting purpose. For that Go to Developer Console->New Lightning Application and use the code mentioned below:

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

Step 5: Test the Application

We have successfully created the application and it’s time to test it. Save and Click on Update Preview to view your created Application. Your page will look something like this.

Now fill in all the details mentioned and Save it. After saving, go to your org to see if the account is created or not. Hope you enjoyed this. Thank You.

Related Articles