Create new record using Lightning Component

By | October 29, 2019

Hi, here we have shown how to insert data in to lead object using Lightning Component.

Apex Controller

public class insertleadrecords {
    @AuraEnabled
    public static void createLead(Lead le){
        insert le; 
    }
}

Component : InsertLeadRecords

<aura:component controller="insertleadrecords"> 
    <aura:attribute name='lea' type='Lead' default="{sobjecttype:'Lead'}"/>
    <lightning:input label='LastName' value='{!v.lea.LastName}'/>
    <lightning:input label='Company' value='{!v.lea.Company}'/>
    <lightning:input label='Status' value='{!v.lea.Status}'/>  
    
    <lightning:button label='submit' onclick='{!c.submitfunction}'/>    
    
</aura:component>

Js Controller

({    
    submitfunction : function(component, event, helper) {
        var lee = component.get('v.lea');
        var a = component.get('c.createLead');
        a.setParams({'le':lee});
        a.setCallback(this,function(r){
            component.set('v.lea',r.getReturnValue());
        });
        $A.enqueueAction(a);
    },    
})

Application

<aura:application extends="force:slds">
    <c:InsertLeadRecords/>
</aura:application>

Output

Enter the data and press on submit button data will be inserted into the sObject

Leave a Reply

Your email address will not be published. Required fields are marked *