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.
Other related post that would you like to learn in Salesforce
Step:-1
submitFormCustomObjectCmp.cmp [Lightning Component]
<aura:component controller="submitFormCustomObjectController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="studentInfoAc" type="student_info__c" default="{'sobjectType': 'student_info__c'}"/>
<aura:attribute name="studentItrt" type="student_info__c[]"/>
<div class="slds">
<div class="slds-col slds-size_6-of-12 slds-m-bottom--small">
<div class="slds-form-element">
<label class="slds-form-element__label">First Name</label>
<div class="slds-form-element__controller"><ui:inputText aura:id="fName" value="{!v.studentInfoAc.student_first_name__c}" class="slds-input" placeholder=""/></div>
</div>
</div>
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_6-of-12 slds-m-bottom--small">
<div class="slds-form-element">
<label class="slds-form-element__label">Last Name</label>
<div class="slds-form-element__controller">
<ui:inputText aura:id="lName" value="{!v.studentInfoAc.student_last_name__c}" class="slds-input" placeholder=""/>
</div>
</div>
</div>
</div>
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_6-of-12 slds-m-bottom--small">
<div class="slds-form-element">
<label class="slds-form-element__label">Phone</label>
<div class="slds-form-element__controller">
<ui:inputText aura:id="phone" value="{!v.studentInfoAc.student_phone__c}" class="slds-input" placeholder=""/>
</div>
</div>
</div>
</div>
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_6-of-12 slds-m-bottom--small">
<div class="slds-form-element">
<label class="slds-form-element__label">Email</label>
<div class="slds-form-element__controller">
<ui:inputText aura:id="emailId" value="{!v.studentInfoAc.student_email__c}" class="slds-input" placeholder=""/>
</div>
</div>
</div>
</div>
<div class="slds-text-align--center slds-m-top--medium">
<button class="slds-button slds-button--brand" onclick="{!c.submitStudentInfo}">Submit</button>
</div>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<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>
<br/><br/>
<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>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<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>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</aura:component>
Step:-2
submitFormCustomObjectCmpController.js [JavaScript File]
({
submitStudentInfo : function(component, event, helper) {
var isValidate = true;
var firstName = component.find('fName');
var firstNameVal = component.find('fName').get('v.value');
if($A.util.isUndefinedOrNull(firstNameVal) || $A.util.isUndefined(firstNameVal) || $A.util.isEmpty(firstNameVal)){
firstName.set("v.errors",[{message:'First Name is required'}]);
isValidate = false;
}else{
firstName.set("v.errors",null);
}
if(isValidate){
var action = component.get('c.getStudent');
var postData = component.get('v.studentInfoAc');
action.setParams({"insertStudent": postData});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var stringItems = response.getReturnValue();
component.set("v.studentItrt",stringItems);
component.set("v.studentInfoAc", null);
//Show the success toast message
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title":"Success",
"type":"success",
"message":"Form submitted successfully.",
});
toastEvent.fire();
}
});
$A.enqueueAction(action);
}
},
})
Step:-3
submitFormCustomObjectCmp.css [Style CSS File]
.THIS {
}
.THIS .slds-input.has-error, .THIS .slds-select.has-error {
background-color: rgb(255, 255, 255);
border-color: rgb(194, 57, 52);
box-shadow: rgb(194, 57, 52) 0 0 0 1px inset;
background-clip: padding-box;
}
Step:-4
submitFormCustomObjectController.apex [Apex Class Controller]
public class submitFormCustomObjectController {
@AuraEnabled
public static List<student_info__c> getStudent(student_info__c insertStudent){
INSERT insertStudent;
RETURN [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c];
}
}
Further post that would you like to learn in Salesforce
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 |