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

3,408 views


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:-

student_first_name__c

student_last_name__c

student_phone__c

student_email__c

 

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.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

 

 

Other related post that would you like to learn in Salesforce

 

Step:-1

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">
  5.  
  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>
  12.  
  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>
  23.  
  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> 
  34.  
  35.  
  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>
  46.  
  47.  
  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>
  51.  
  52. <br/><br/>
  53.    <!--Start RelatedTopics Section-->
  54. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  55.  
  56.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" 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="https://www.w3web.net/" 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>
  57.  
  58.             <br/><br/>
  59.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" 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="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  64.                         <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  65.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  66.                         <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  67.                         <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  68.                     </ul>
  69.             </div>
  70.  
  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="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  74.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  75.                         <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  76.                         <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  77.                         <li><a href="https://www.w3web.net/update-parent-object-from-child/" 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="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" 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>
  86.  
  87. </div>
  88.  
  89.   <!--End RelatedTopics Section-->
  90.  
  91.  
  92.     </div>
  93.  
  94. </aura:component>

Step:-2

submitFormCustomObjectCmpController.js [JavaScript File]

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.    ({
  2. 	submitStudentInfo : function(component, event, helper) {
  3.  
  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.         }        
  13.  
  14.         if(isValidate){
  15.              var action = component.get('c.getStudent');
  16.              var postData =  component.get('v.studentInfoAc');                
  17.              action.setParams({"insertStudent": postData});
  18.  
  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);       
  25.  
  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.                 toastEvent.fire();            
  34.  
  35.             }
  36.  
  37.         }); 
  38.  
  39.         $A.enqueueAction(action);
  40.  
  41.         }
  42.  
  43.     },
  44.  
  45. })

Step:-3

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.   }

Step:-4

submitFormCustomObjectController.apex [Apex Class Controller]

  1.   public class submitFormCustomObjectController {
  2.  
  3.     @AuraEnabled
  4.     public static List<student_info__c> getStudent(student_info__c insertStudent){
  5.         INSERT insertStudent;
  6.  
  7.         RETURN [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c];
  8.     }
  9.  
  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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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 w3web.net. 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 w3web.net

Leave a Comment