How to create a form and submit on database in lightning component? | how to building a form with multiple lightning input field in aura components and submit through apex class in Salesforce


Hey guys, today in this post we are going to learn about How to create a form and submit on database in Salesforce lightning component.

We will be achieve to validate a form and submit into custom sObject in Salesforce.

We used the files in this example:-

File Name File Type Description
submitFormCustomObjectCmp.cmp Lightning Component It is hold User Interface (UI) tags and display on the browser.
submitFormCustomObjectCmpController.js JavaScript File It is used for form validation and javascript function to call a apex class method.
submitFormCustomObjectCmp.css Style CSS File For alignment and form validation input border color.
submitFormCustomObjectController.apex Apex Class Controller For create a record in database server

Custom Object:- student_info__c

Custom Object Fields:-






Custom Object and their fields Custom object like database and custom fields are column of data table.


You can download file directly from github by Click Here.



Other related post that would you like to learn in Salesforce



submitFormCustomObjectCmp.cmp [Lightning Component]

  1.   <aura:component controller="submitFormCustomObjectController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
  2. 	<aura:attribute name="studentInfoAc" type="student_info__c" default="{'sobjectType': 'student_info__c'}"/>
  3.      <aura:attribute name="studentItrt" type="student_info__c[]"/>    
  4.     <div class="slds">
  6.             <div class="slds-col slds-size_6-of-12  slds-m-bottom--small">
  7.                 <div class="slds-form-element">
  8.                    <label class="slds-form-element__label">First Name</label>
  9.                    <div class="slds-form-element__controller"><ui:inputText aura:id="fName" value="{!v.studentInfoAc.student_first_name__c}" class="slds-input" placeholder=""/></div>
  10.                 </div>    
  11.             </div>
  13.            <div class="slds-grid slds-wrap"> 
  14.                <div class="slds-col slds-size_6-of-12  slds-m-bottom--small">
  15.                  <div class="slds-form-element"> 
  16.                         <label class="slds-form-element__label">Last Name</label>
  17.                         <div class="slds-form-element__controller">
  18.                             <ui:inputText aura:id="lName" value="{!v.studentInfoAc.student_last_name__c}" class="slds-input" placeholder=""/>
  19.                          </div>
  20.                   </div> 
  21.               </div> 
  22.            </div>
  24.               <div class="slds-grid slds-wrap"> 
  25.                <div class="slds-col slds-size_6-of-12  slds-m-bottom--small">
  26.                  <div class="slds-form-element"> 
  27.                         <label class="slds-form-element__label">Phone</label>
  28.                         <div class="slds-form-element__controller">
  29.                             <ui:inputText aura:id="phone" value="{!v.studentInfoAc.student_phone__c}" class="slds-input" placeholder=""/>
  30.                          </div>
  31.                   </div> 
  32.               </div> 
  33.            </div> 
  36.          <div class="slds-grid slds-wrap"> 
  37.                <div class="slds-col slds-size_6-of-12  slds-m-bottom--small">
  38.                  <div class="slds-form-element"> 
  39.                         <label class="slds-form-element__label">Email</label>
  40.                         <div class="slds-form-element__controller">
  41.                             <ui:inputText aura:id="emailId" value="{!v.studentInfoAc.student_email__c}" class="slds-input" placeholder=""/>
  42.                          </div>
  43.                   </div> 
  44.               </div> 
  45.            </div>
  48.         <div class="slds-text-align--center slds-m-top--medium">
  49.             <button class="slds-button slds-button--brand" onclick="{!c.submitStudentInfo}">Submit</button>
  50.         </div>
  52. <br/><br/>
  53.    <!--Start RelatedTopics Section-->
  54. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  56.             <p data-aura-rendered-by="435:0"><img src="" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  58.             <br/><br/>
  59.             <p data-aura-rendered-by="435:0"><img src="" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like β†’</span> </strong></p>
  60.             <div style="display:block; overflow:hidden;"> 
  61.                 <div style="width: 50%; float:left; display:inline-block">
  62.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  63.                         <li><a href="" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  64.                         <li><a href="" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  65.                         <li><a href="" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  66.                         <li><a href="" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  67.                         <li><a href="" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  68.                     </ul>
  69.             </div>
  71.             <div style="width: 50%; float:left; display:inline-block">
  72.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  73.                         <li><a href="" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  74.                         <li><a href="" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  75.                         <li><a href="" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  76.                         <li><a href="" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  77.                         <li><a href="" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  78.                     </ul>
  79.                 </div>
  80.                <div style="clear:both;"></div> 
  81.                <br/>
  82.                 <div class="youtubeIcon">
  83.                     <a href="" target="_blank" rel="noopener noreferrer"><img src="" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  84.                 </div>
  85.     </div>
  87. </div>
  89.   <!--End RelatedTopics Section-->
  92.     </div>
  94. </aura:component>


submitFormCustomObjectCmpController.js [JavaScript File]

  1.    ({
  2. 	submitStudentInfo : function(component, event, helper) {
  4.         var isValidate = true;        
  5.         var firstName = component.find('fName');        
  6.         var firstNameVal = component.find('fName').get('v.value');        
  7.         if($A.util.isUndefinedOrNull(firstNameVal) || $A.util.isUndefined(firstNameVal) || $A.util.isEmpty(firstNameVal)){
  8.             firstName.set("v.errors",[{message:'First Name is required'}]);
  9.             isValidate = false;
  10.         }else{
  11.             firstName.set("v.errors",null);
  12.         }        
  14.         if(isValidate){
  15.              var action = component.get('c.getStudent');
  16.              var postData =  component.get('v.studentInfoAc');                
  17.              action.setParams({"insertStudent": postData});
  19.             action.setCallback(this, function(response) {            
  20.             var state = response.getState();  
  21.             if (state === "SUCCESS") {                
  22.                 var stringItems = response.getReturnValue();
  23.                 component.set("v.studentItrt",stringItems);
  24.                 component.set("v.studentInfoAc", null);       
  26.                 //Show the success toast message
  27.                 var toastEvent = $A.get("e.force:showToast");
  28.                 toastEvent.setParams({
  29.                     "title":"Success",
  30.                     "type":"success",
  31.                     "message":"Form submitted successfully.",                        
  32.                 });
  33.       ;            
  35.             }
  37.         }); 
  39.         $A.enqueueAction(action);
  41.         }
  43.     },
  45. })


submitFormCustomObjectCmp.css [Style CSS File]

  1.   .THIS {
  2. }
  3. .THIS .slds-input.has-error, .THIS .slds-select.has-error {
  4.     background-color: rgb(255, 255, 255);
  5.     border-color: rgb(194, 57, 52);
  6.     box-shadow: rgb(194, 57, 52) 0 0 0 1px inset;
  7.     background-clip: padding-box;
  8.   }


submitFormCustomObjectController.apex [Apex Class Controller]

  1.   public class submitFormCustomObjectController {
  3.     @AuraEnabled
  4.     public static List<student_info__c> getStudent(student_info__c insertStudent){
  5.         INSERT insertStudent;
  7.         RETURN [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c];
  8.     }
  10. }


Further post that would you like to learn in Salesforce



FAQ (Frequently Asked Questions)

Why Apex form is used?

apex:form, it is a section of visualforce page that allows users to enter input and then submit it with command button or link. It is a best practice to use only one apex:form tag in a page or custom component.

How do I create a registration form in Salesforce?

Select Administration, then select Login & Registration. Under Registration Page Configuration, select Allow customers and partners to self-register. Notice that the page expands to display the Registration settings populated by the Customer Service template. For Profile, select Customers.

What is self-registration in Salesforce?

The Self-Registration component lets users set up their accounts so that you don't have to add each one manually. Users put their information into the form fields, and when they click Sign Up, an account is created for them. Enter the path to the page that users see after they log in.

Related Topics | You May Also Like

Our Free Courses β†’

πŸ‘‰ Get Free Course β†’

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration


Hi, This is Vijay Kumar behind the admin and founder of I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on

Leave a Comment