Hey guys, In this post we are going to learn that how we can create a custom record search and delete record functionality in Salesforce lightning components.
In this example we will search Custom sObject records with an lightning input field.
Files we used in this post example:-
File Name | File Type | Description |
---|---|---|
studentInfoApp.app | Lightning Component Application | It is used for preview the component on browser. |
studentInfoCmp.cmp | Lightning Component | It is used for Search Inpur field and Record User InterfaceTable. |
studentInfoCmpController.js | JavaScript Controller File | It is used for search record and deleter record functionality. |
studentInfoCmpHelper.js | JavaScript Helper File | Helper function called in the Javascript controller on Init function. |
studentInfoCtrl.apxc | Apex Class Controller | It is used for For Search and delete record from database server |
Custom Object:- student_info__c Custom Object Fields:- student_first_name__c student_last_name__c student_phone__c
|
Custom Object and their fields | Custom object like database and custom fields are column of data table. |
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Lightning Application →
Step 1:- Create Lightning Application : studentInfoApp.app
From Developer Console >> File >> New >> Lightning Application
studentInfoApp.app [Component Application File]
<aura:application extends="force:slds">
<c:studentInfoCmp />
</aura:application>
Create Lightning Component →
Step 2:- Create Lightning Component : studentInfoCmp.cmp
From Developer Console >> File >> New >> Lightning Component
studentInfoCmp.cmp [Lightning Component File]
<aura:component controller="studentInfoCtrl" 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:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="studentItrt" type="student_info__c[]"/>
<div class="slds slds-p-horizontal--medium">
<div class="slds-large-size--3-of-12 slds-m-bottom--medium slds-p-around--small">
Search Student Name <ui:inputText aura:id="searchFld" value="" class="slds-input" keyup="{!c.searchRow}" updateOn="keyup" placeholder="Search Name..."/>
</div>
<div style="width:40%;">
<table class="slds-table slds-table--bordered">
<tr style="background-color:#eee;">
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th></th>
</tr>
<aura:iteration items="{!v.studentItrt}" var="studentItem" >
<tr>
<td>{!studentItem.student_first_name__c} </td>
<td>{!studentItem.student_last_name__c}</td>
<td>{!studentItem.student_phone__c}</td>
<td><span><a style="color:#ff0000; font-weight:bold;" id="{!studentItem.Id}" onclick="{!c.deleteRow}" title="Delete">X</a></span></td>
</tr>
</aura:iteration>
</table>
</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>
Create JavaScript Controller →
Step 3:- Create Lightning Component : JavaScript Controller
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
studentInfoCmpController.js [JavaScript Controller]
({
doInit: function(component, event, helper) {
helper.loadDetails(component, event);
},
submitStudentInfo : function(component, event, helper) {
var action = component.get('c.getStudent');
var postData = component.get('v.studentInfoAc');
console.log(':: postData ::: '+JSON.stringify(postData));
console.log(':: postData Name :: '+postData.student_first_name__c);
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.student_first_name__c', "");
component.set('v.studentInfoAc.student_last_name__c', "");
component.set('v.studentInfoAc.student_phone__c', "");
}
});
$A.enqueueAction(action);
},
deleteRow : function(component, event, helper) {
var thisObj = event.target.id;
var action = component.get('c.deleteRowCtrl');
action.setParams({'deleteRowId':thisObj});
action.setCallback(this, function(response){
var state = response.getState();
if(state == 'SUCCESS'){
var getReturnRow = response.getReturnValue();
component.set('v.studentItrt', getReturnRow);
//component.set('v.studentInfoAc', []);
alert('Record deleted successfully');
}
});
$A.enqueueAction(action);
},
searchRow : function(component, event, helper) {
var searchFld = component.find('searchFld').get("v.value");
var action = component.get('c.searchData');
action.setParams({'searchKey':searchFld});
action.setCallback(this, function(response){
var state = response.getState();
if(state == 'SUCCESS'){
var getReturnRow = response.getReturnValue();
component.set('v.studentItrt', getReturnRow);
}
});
$A.enqueueAction(action);
},
})
Create JavaScript Helper →
Step 4:- Create Lightning Component : studentInfoCmpHelper.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Helper
studentInfoCmpHelper.js [JavaScript Helper File]
({
loadDetails: function(component, event) {
var studentItrt = component.get('v.studentItrt');
var action = component.get('c.loadStudentInfo');
action.setCallback(this, function(response){
var state = response.getState();
if(state == 'SUCCESS'){
var getResponse = response.getReturnValue();
component.set('v.studentItrt',getResponse);
console.log('studentItrt::' + studentItrt);
}
});
$A.enqueueAction(action);
},
})
Create Apex Class Controller →
Step 5:- Create Apex Class : studentInfoCtrl.apxc
From Developer Console >> File >> New >> Apex Class
studentInfoCtrl.apxc [Apex Class Controller]
public class studentInfoCtrl {
@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];
}
@AuraEnabled
public static List<student_info__c> loadStudentInfo(){
RETURN [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c];
}
@AuraEnabled
public static List<student_info__c> deleteRowCtrl(String deleteRowId){
DELETE [SELECT Id FROM student_info__c WHERE Id=:deleteRowId];
List<student_info__c> getRowDel = [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c];
RETURN getRowDel;
}
@AuraEnabled
public static List<student_info__c> fetchMyData(String searchKey) {
//system.debug('1-->searchKey '+searchKey+'====fieldName :'+fieldName+'======userProfile :'+userProfile);
searchKey = '%'+searchKey+'%';
List<student_info__c> regList = [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c];
RETURN regList;
}
@AuraEnabled
public static List<student_info__c> searchData(String searchKey) {
//system.debug('1-->searchKey '+searchKey+'====fieldName :'+fieldName+'======userProfile :'+userProfile);
searchKey = '%'+searchKey+'%';
List<student_info__c> regList = [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c WHERE student_first_name__c LIKE: searchKey];
RETURN regList;
}
}
Further post that would you like to learn in Salesforce
How do I remove all records from an object?
To delete all records from a custom object, just navigate to the custom object definition, click on 'Truncate' and that's it.
How do I delete my aura record?
To delete a record using Lightning Data Service, call deleteRecord on the force:recordData component, and pass in a callback function to be invoked after the delete operation completes.
How do I delete a record in LWC list?
To delete records in LWC, we first need to import fetchOpportunities method from Apex Controller using @salesforce/apex/ module in JS Controller. Then, include deleteRecord method from lightning/uiRecordApi module.
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 |