Custom record search and delete functionality in lightning component | create custom search functionality with add/delete row dynamically on click icon button in Salesforce lightning component

1,596 views

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

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

 

Search and delete record functionality in lightning component -- w3web.net

 

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]

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.    <aura:application extends="force:slds">	
  2.        <c:studentInfoCmp />
  3.    </aura:application>

 

Create Lightning Component →

Step 2:- Create Lightning Component : studentInfoCmp.cmp

From Developer Console >> File >> New >> Lightning Component

studentInfoCmp.cmp [Lightning Component File]

  1.    <aura:component controller="studentInfoCtrl" 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:handler name="init" value="{!this}" action="{!c.doInit}"/>    
  4.     <aura:attribute name="studentItrt" type="student_info__c[]"/> 
  5.  
  6.   <div class="slds slds-p-horizontal--medium">  
  7.     <div class="slds-large-size--3-of-12 slds-m-bottom--medium slds-p-around--small">
  8.         Search Student Name <ui:inputText aura:id="searchFld" value="" class="slds-input" keyup="{!c.searchRow}" updateOn="keyup" placeholder="Search Name..."/>
  9.     </div>    
  10.  
  11.    <div style="width:40%;"> 
  12.     <table class="slds-table slds-table--bordered">        
  13.         <tr style="background-color:#eee;">
  14.             <th>First Name</th>
  15.             <th>Last Name</th>
  16.             <th>Phone</th>  
  17.             <th></th>
  18.         </tr>
  19.  
  20.         <aura:iteration items="{!v.studentItrt}" var="studentItem" >
  21.             <tr>
  22.                 <td>{!studentItem.student_first_name__c} </td>
  23.                 <td>{!studentItem.student_last_name__c}</td>
  24.                 <td>{!studentItem.student_phone__c}</td>
  25.                 <td><span><a style="color:#ff0000; font-weight:bold;" id="{!studentItem.Id}" onclick="{!c.deleteRow}" title="Delete">X</a></span></td>
  26.             </tr>
  27.         </aura:iteration>      
  28.  
  29.  
  30.     </table>
  31.    </div> 
  32.  
  33. <br/><br/>
  34.    <!--Start RelatedTopics Section-->
  35. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  36.  
  37.             <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>
  38.  
  39.             <br/><br/>
  40.             <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>
  41.             <div style="display:block; overflow:hidden;"> 
  42.                 <div style="width: 50%; float:left; display:inline-block">
  43.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  44.                         <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>
  45.                         <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>
  46.                         <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>
  47.                         <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>
  48.                         <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>
  49.                     </ul>
  50.             </div>
  51.  
  52.             <div style="width: 50%; float:left; display:inline-block">
  53.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  54.                         <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>
  55.                         <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>
  56.                         <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>
  57.                         <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>
  58.                         <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>
  59.                     </ul>
  60.                 </div>
  61.                <div style="clear:both;"></div> 
  62.                <br/>
  63.                 <div class="youtubeIcon">
  64.                     <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>
  65.                 </div>
  66.     </div>
  67.  
  68. </div>
  69.  
  70.   <!--End RelatedTopics Section-->
  71.  
  72.  
  73.   </div>  
  74.  </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]

  1.       ({
  2.  
  3.     doInit: function(component, event, helper) {        
  4.         helper.loadDetails(component, event);
  5.  
  6.     },
  7.  
  8.  
  9.     submitStudentInfo : function(component, event, helper) {
  10.         var action = component.get('c.getStudent');
  11.         var postData =  component.get('v.studentInfoAc');        
  12.         console.log(':: postData ::: '+JSON.stringify(postData));
  13.         console.log(':: postData Name :: '+postData.student_first_name__c);
  14.         action.setParams({"insertStudent": postData});   
  15.  
  16.         action.setCallback(this, function(response) {            
  17.             var state = response.getState();  
  18.             if (state === "SUCCESS") {                
  19.                 var stringItems = response.getReturnValue();
  20.                 component.set("v.studentItrt",stringItems);
  21.                 component.set('v.studentInfoAc.student_first_name__c', ""); 
  22.                 component.set('v.studentInfoAc.student_last_name__c', "");
  23.                component.set('v.studentInfoAc.student_phone__c', ""); 
  24.             }
  25.         }); 
  26.  
  27.         $A.enqueueAction(action);
  28.     },
  29.  
  30.  
  31.     deleteRow : function(component, event, helper) {
  32. 	   var thisObj = event.target.id;        
  33.        var action = component.get('c.deleteRowCtrl');
  34.         action.setParams({'deleteRowId':thisObj});
  35.         action.setCallback(this, function(response){
  36.             var state = response.getState();
  37.             if(state == 'SUCCESS'){
  38.                 var getReturnRow =  response.getReturnValue();
  39.                 component.set('v.studentItrt', getReturnRow); 
  40.                 //component.set('v.studentInfoAc', []);
  41.                 alert('Record deleted successfully');
  42.             }
  43.         });
  44.         $A.enqueueAction(action);
  45. 	},
  46.  
  47.  
  48.     searchRow : function(component, event, helper) {	
  49.         var searchFld = component.find('searchFld').get("v.value");
  50.  
  51.        var action = component.get('c.searchData');
  52.         action.setParams({'searchKey':searchFld});
  53.         action.setCallback(this, function(response){
  54.             var state = response.getState();
  55.             if(state == 'SUCCESS'){
  56.                 var getReturnRow =  response.getReturnValue();
  57.                 component.set('v.studentItrt', getReturnRow);
  58.             }
  59.  
  60.         });
  61.         $A.enqueueAction(action);
  62. 	},
  63.  
  64. })

 

Create JavaScript Helper →

Step 4:- Create Lightning Component : studentInfoCmpHelper.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Helper

JavaScript helper function in Salesforce by w3web.net

studentInfoCmpHelper.js [JavaScript Helper File]

  1.       ({
  2.  
  3.     loadDetails: function(component, event) {
  4.        var studentItrt =   component.get('v.studentItrt');
  5.        var action =   component.get('c.loadStudentInfo');
  6.         action.setCallback(this, function(response){
  7.           var state = response.getState();
  8.         if(state == 'SUCCESS'){
  9.             var getResponse = response.getReturnValue();
  10.             component.set('v.studentItrt',getResponse);
  11.             console.log('studentItrt::' + studentItrt);
  12.           }
  13.         });
  14.  
  15.       $A.enqueueAction(action);
  16.  
  17.     },
  18.  
  19.     })

 

 

Create Apex Class Controller →

Step 5:- Create Apex Class : studentInfoCtrl.apxc

From Developer Console >> File >> New >> Apex Class

studentInfoCtrl.apxc [Apex Class Controller]

  1.    public class studentInfoCtrl {
  2.  
  3.  
  4.     @AuraEnabled
  5.     public static List<student_info__c> getStudent(student_info__c insertStudent){
  6.         INSERT insertStudent;
  7.  
  8.         RETURN [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c];
  9.     }
  10.  
  11.  
  12.     @AuraEnabled
  13.     public static List<student_info__c> loadStudentInfo(){
  14.         RETURN [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c];
  15.     }
  16.  
  17.  
  18.     @AuraEnabled
  19.     public static List<student_info__c> deleteRowCtrl(String deleteRowId){
  20.         DELETE [SELECT Id FROM student_info__c WHERE Id=:deleteRowId];
  21.         List<student_info__c> getRowDel = [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c]; 
  22.         RETURN getRowDel;
  23.     }
  24.  
  25.  
  26.  
  27.     @AuraEnabled
  28.     public static List<student_info__c> fetchMyData(String searchKey) { 
  29.         //system.debug('1-->searchKey '+searchKey+'====fieldName :'+fieldName+'======userProfile :'+userProfile); 
  30.         searchKey = '%'+searchKey+'%';
  31.  
  32.         List<student_info__c> regList = [SELECT Id, student_first_name__c,student_last_name__c,student_phone__c FROM student_info__c]; 
  33.         RETURN regList;
  34.     }    
  35.  
  36.  
  37.     @AuraEnabled
  38.     public static List<student_info__c> searchData(String searchKey) { 
  39.         //system.debug('1-->searchKey '+searchKey+'====fieldName :'+fieldName+'======userProfile :'+userProfile); 
  40.         searchKey = '%'+searchKey+'%';
  41.  
  42.         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]; 
  43.         RETURN regList;
  44.     }
  45.  
  46.  
  47. }

 

 

Further post that would you like to learn in Salesforce

 
 

 

 

FAQ (Frequently Asked Questions)

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

 
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