Hey guys, today in this post we are going to learn about how to Insert New Record with Radio Button, Checkbox, Date picker, Picklist, Long Text Area Using Lightning Component and Apex Controller.
- Don’t forget to check out:- Deleting multiple records dynamically with checkbox on delete button in LWC Click Here For More Information
We used the files in this example:-
submitFormCustomObjApp.app | Lightning Application | It is used for call the component to preview on browser. |
submitFormCustomObjCmp.cmp |
Lightning Component | It is used for create a custom form on lightning component . |
submitFormCustomObjCmpController.js |
JavaScript Controller File | It is used for click function for insert a form input value in database. |
submitFormCustomObjCmpHelper.js | JavaScript Controller HelperFile | It is used for fatch the picklist value from server of State and City |
submitFormCustomObjCmp.css | Component Style CSS | It is used for create customΒ Style CSSΒ for alignment the form input field and section. |
submitFormCustomObjCmpCtrl.apxc | Apex Class Controller | It is used for insert input value into database server |
Live Demo
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Step 1:- Create Lightning Application : submitFormCustomObjApp.app
From Developer Console >> File >> New >> Lightning Application
submitFormCustomObjApp.app [Component Application File]
<aura:application extends="force:slds">
<c:submitFormCustomObjCmp/>
</aura:application>
Step 2:- Create Lightning Component : submitFormCustomObjCmp.cmp
From Developer Console >> File >> New >> Lightning Component
submitFormCustomObjCmp.cmp [Lightning Component File]
<aura:component controller="submitFormCustomObjCmpCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="newStudent" type="NewStudent__c" default="{'sobjectType' : 'NewStudent__c'}"/>
<aura:attribute name="cityListStr" type="string[]" />
<aura:attribute name="stateListStr" type="string[]" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<div class="slds">
<div class="slds-grid slds-wrap">
<div class="slds-col--padded-large slds-large-size--6-of-12 slds-medium-size--6-of-12 slds-small-size--12-of-12 slds-x-small-size--12-of-12 slds-m-top--small">
<div class="slds-form-element slds-is-required">
<label class="slds-form-element__label">Student Name</label>
<ui:inputText class="slds-input" aura:id="studentName" value="{!v.newStudent.Name}"/>
</div>
</div>
<div class="slds-col--padded-large slds-large-size--6-of-12 slds-medium-size--6-of-12 slds-small-size--12-of-12 slds-x-small-size--12-of-12 slds-m-top--small">
<div class="slds-form-element slds-is-required">
<label class="slds-form-element__label">Email</label>
<ui:inputEmail class="slds-input" aura:id="emailId" value="{!v.newStudent.Email__c}"/>
</div>
</div>
<div class="slds-col--padded-large slds-large-size--6-of-12 slds-medium-size--6-of-12 slds-small-size--12-of-12 slds-x-small-size--12-of-12 slds-m-top--small">
<div class="slds-form-element slds-is-required">
<label class="slds-form-element__label">State</label>
<div class="slds-form-element__controller">
<ui:inputSelect class="slds-select" aura:id="stateName" change="{!c.changeStateName}" value="{!v.newStudent.State__c}">
<ui:inputSelectOption text="--None--" />
<aura:iteration items="{!v.stateListStr}" var="stateVal">
<ui:inputSelectOption text="{!stateVal}" />
</aura:iteration>
</ui:inputSelect>
</div>
</div>
</div>
<div class="slds-col--padded-large slds-large-size--6-of-12 slds-medium-size--6-of-12 slds-small-size--12-of-12 slds-x-small-size--12-of-12 slds-m-top--small">
<div aura:id="idSearchboxProductionExecutives" class="slds-form-element slds-is-required">
<label class="slds-form-element__label">City</label>
<div class="slds-form-element__controller">
<ui:inputSelect class="slds-select" aura:id="cityName" change="{!c.changeCityName}" value="{!v.newStudent.City__c}">
<ui:inputSelectOption text="--None--" />
<aura:iteration items="{!v.cityListStr}" var="cityVal">
<ui:inputSelectOption text="{!cityVal}" />
</aura:iteration>
</ui:inputSelect>
</div>
</div>
</div>
<div class="slds-col--padded-large slds-large-size--6-of-12 slds-medium-size--6-of-12 slds-small-size--12-of-12 slds-x-small-size--12-of-12 slds-m-top--small">
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__controller">
<lightning:input type="date" name="dateOfBirth" aura:id="dateOfBirth" value="{!v.newStudent.DateOfBirth__c}" label="Date Of Birth" labelClass="slds-form-element__label"/>
</div>
</div>
</div>
<div class="slds-col--padded-large slds-large-size--6-of-12 slds-medium-size--6-of-12 slds-small-size--12-of-12 slds-x-small-size--12-of-12 slds-m-top--small">
<div class="slds-form-element slds-is-required">
<label class="slds-form-element__label">Gender</label>
<div class="slds-form-element__controller">
<div class="slds-grid slds-wrap slds-m-bottom--small">
<label class="slds-radio">
<input type="radio" id="maleOption" class="slds-m-right--medium" name="options" value="Male" checked="{!equals(v.newStudent.Gender__c,'Male')}" onclick="{!c.setGenderValue}"/>
<span class="slds-radio--faux"></span>
<span class="slds-form-element__label"> Male</span>
</label>
<label class="slds-radio">
<input type="radio" id="femaleOption" name="options" value="Female" checked="{!equals(v.newStudent.Gender__c,'Female')}" onclick="{!c.setGenderValue}"/>
<span class="slds-radio--faux"></span>
<span class="slds-form-element__label"> Female</span>
</label>
</div>
</div>
</div>
</div>
<div class="slds-col--padded-large slds-large-size--6-of-12 slds-medium-size--6-of-12 slds-small-size--12-of-12 slds-x-small-size--12-of-12 slds-m-top--small">
<div class="slds-form-element slds-is-required">
<label class="slds-form-element__label">Is Married?</label>
<div class="slds-form-element__controller">
<lightning:input type="checkbox" aura:id="checkbox" name="IsMarried" onchange="{!c.onCheck}"/>
</div>
</div>
<aura:if isTrue="{!v.newStudent.IsMarried__c}">
<div class="slds-form-element slds-is-required slds-m-top--small">
<label class="slds-form-element__label">Spouse Name</label>
<ui:inputText class="slds-input" aura:id="spouseName" value="{!v.newStudent.SpouseName__c}" />
</div>
</aura:if>
</div>
<div class="slds-col--padded-large slds-large-size--6-of-12 slds-medium-size--6-of-12 slds-small-size--12-of-12 slds-x-small-size--12-of-12 slds-m-top--small">
<div class="slds-form-element slds-is-required">
<label class="slds-form-element__label">Address</label>
<ui:inputTextArea aura:id="address" class="slds-textarea" value="{!v.newStudent.Address__c}" rows="2"/>
</div>
</div>
</div>
<div class="slds-text-align--center slds-m-top--medium slds-m-bottom--medium">
<button class="slds-button slds-button--brand" onclick="{!c.submitAction}">Submit</button>
</div>
<br/>
<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 3:- Create Lightning Component : submitFormCustomObjCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
submitFormCustomObjCmpController.js [JavaScript Controller]
({
doInit:function(component, event, helper){
helper.cityPickListView(component);
helper.statePickListView(component);
},
submitAction : function(component, event, helper) {
var action = component.get("c.newStudentMethod");
var newStudent = component.get('v.newStudent');
var checkbox = component.find('checkbox').get('v.checked');
var validate = true;
var studentName = component.find('studentName');
var studentNameVal = component.find('studentName').get('v.value');
var emailVal = component.get('v.newStudent.Email__c');
if($A.util.isUndefinedOrNull(studentNameVal) || $A.util.isUndefined(studentNameVal) || $A.util.isEmpty(studentNameVal)){
studentName.set("v.errors",[{message:"Please select required field"}]);
validate = false;
}else{
studentName.set("v.errors",null);
}
if(validate){
action.setParams({"studentRecId":newStudent});
action.setCallback(this, function(response){
var state = response.getState();
if(state == "SUCCESS"){
var result = response.getReturnValue();
//alert('result ' + result);
component.set('v.newStudent.Name','');
component.set('v.newStudent.Email__c','');
component.set('v.newStudent.State__c','--None--');
component.set('v.newStudent.City__c','--None--');
component.set('v.newStudent.DateOfBirth__c','');
component.set('v.newStudent.SpouseName__c','');
component.set('v.newStudent.Address__c','');
component.set('v.newStudent.Gender__c',null);
component.find('checkbox').set('v.checked',false);
/*
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"type":'success',
"message": "The record has been updated successfully."
});
toastEvent.fire();
*/
alert('Record saved successfully');
}
});
$A.enqueueAction(action);
}
},
changeCityName:function(component, event, helper){
var cityName = component.find('cityName').get('v.value');
component.set('v.newStudent.City__c',cityName);
},
changeStateName:function(component, event, helper){
var stateName = component.find('stateName').get('v.value');
component.set('v.newStudent.State__c',stateName);
},
setCheckBoxValue: function(component,event) {
var val = event.currentTarget.value;
component.set("v.newStudent.IsMarried__c",val);
},
onCheck: function(component,event) {
var checkbox = component.find('checkbox').get('v.checked');
component.set("v.newStudent.IsMarried__c",checkbox);
},
setGenderValue: function(component,event) {
var val = event.currentTarget.value;
//alert(val);
component.set("v.newStudent.Gender__c",val);
},
})
Step 4:- Create Lightning Component : submitFormCustomObjCmpHelper.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Helper
submitFormCustomObjCmpHelper.js [JavaScript Helper File]
({
cityPickListView : function(component, event, helper) {
var action = component.get("c.pickList");
action.setParams({"obj":"NewStudent__c","str":"City__c"});
action.setCallback(this, function(response){
var state = response.getState();
if(state=="SUCCESS"){
var result = response.getReturnValue();
component.set('v.cityListStr',result);
}
});
$A.enqueueAction(action);
},
statePickListView : function(component, event, helper) {
var action = component.get("c.pickList");
action.setParams({"obj":"NewStudent__c","str":"State__c"});
action.setCallback(this, function(response){
var state = response.getState();
if(state=="SUCCESS"){
var result = response.getReturnValue();
component.set('v.stateListStr',result);
}
});
$A.enqueueAction(action);
},
})
Step 5:- Create Lightning Component Style: submitFormCustomObjCmp.css
From Developer Console >> File >> New >> Lightning Component >> Component Style CSS
submitFormCustomObjCmp.css [Style CSS]
.THIS {
background:#fff !important;
}
.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 6:- Create Apex Class : submitFormCustomObjCmpCtrl
From Developer Console >> File >> New >> Apex Class
studentInfoCtrl.apxc [Apex Class Controller]
public class submitFormCustomObjCmpCtrl {
@AuraEnabled
public static void newStudentMethod(NewStudent__c studentRecId){
upsert studentRecId;
}
@AuraEnabled
public static string newStudentInsrt(string nameStr, string emailStr){
NewStudent__c stdntNameStr = NEW NewStudent__c();
List<NewStudent__c> stdntNameList=[SELECT Id, Name FROM NewStudent__c WHERE Name =: nameStr];
IF(stdntNameList.size() > 0){
RETURN 'Duplicate name Not Allow';
}ELSE{
stdntNameStr.Name=nameStr;
stdntNameStr.Email__c=emailStr;
INSERT stdntNameStr;
RETURN 'Inserted successfully';
}
}
@AuraEnabled
public static List<String> pickList(String obj, String str) {
List<String> regList = NEW List<String>();
Schema.DescribeFieldResult plistvalues = Schema.getGlobalDescribe().get(obj).getDescribe().fields.getMap().get(str).getDescribe();
FOR(PicklistEntry ent:plistvalues.getpicklistvalues()) {
regList.add(ent.getLabel());
}
RETURN regList;
}
}
Further post that would you like to learn in Salesforce
How do you display record details with lightning component?
To display a record using lightning:recordForm , provide the record ID and the object API name. Additionally, provide fields using either the fields or layoutType attribute. You can display a record in two modes using the mode attribute. Loads the form using output fields with inline editing enabled.
How do you add a new record with lightning component?
To create a record using force:recordData , leave out the recordId attribute. Load a record template by calling the getNewRecord function on force:recordData . Finally, apply values to the new record, and save the record by calling the saveRecord function on force:recordData
How do you show radio buttons horizontally in lightning?
Use label-inline to horizontally align the label and radio group. Use label-stacked to place the label above the radio group.
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 |