Create a record Using Aura Component

In this blog, I showed the code for creating records in the aura component may be this source code helps you a lot to understand the concept of the aura component.

 

Create an apex file (Name : createContactController.apxc)

Source Code: createContactController.apxc

				
					public class GetContactController {
    @AuraEnabled
    public static void insertCon(String firstName,String lastName,String phoneNo, String emailId){
        Contact con = new Contact();
        con.FirstName = firstName;
        con.LastName = lastName;
        con.Phone = phoneNo;
        con.Email = emailId;
        insert con;
    }
}
				
			

Create an Aura component (Name : createContactCMP)

Source Code: createContactCMP.cmp

				
					<aura:component controller="createContactController">
    <div class="slds">
        <lightning:input type="text" aura:id="firstName" label="Enter First Name" />
        <lightning:input type="text" aura:id="lastName" label="Enter Last Name" />
        <lightning:input type="text" aura:id="phone" label="Enter Phone Number" />
        <lightning:input type="text" aura:id="email" label="Enter Email" />
        <lightning:button variant="brand"
                                          label="Create Contact"
                                          title="Create Contact"
                                          onclick="{!c.createContact}"/>
    </div>
</aura:component>
				
			

Source Code: createContactCMPController.js

				
					({
	createContact : function(component, event, helper){
    var firstNameJs = component.find("firstName").get("v.value");
    var lastNameJs = component.find("lastName").get("v.value");
    var phone = component.find("phone").get("v.value");
    var email = component.find("email").get("v.value");
    var action = component.get("c.insertCon");
        action.setParams({'firstName' : firstNameJs, 'lastName' : lastNameJs, 'phoneNo' : phone, 'emailId' : email});
        action.setCallback(this,function(response){
            if(response.getState() == 'SUCCESS'){
                alert('Record Inserted!');
            }
        });
        $A.enqueueAction(action);
}
})
				
			

Create a lightning application (Name: CreateContactApp)

Source Code: CreateContactApp.app

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

Leave a Comment