How to retrieve data from multiple object and delete cross object records of selected row using apex wrapper class lightning component in Salesforce | how to delete cross object records of selected row

1,566 views

Hey guys, today in this post we are going to learn about how to retrieve data from multiple object and delete cross object records of selected row using apex wrapper class lightning component in salesforce

 

Files we used in this post example β†’

deleteNestedQueryWrapperCmp.cmp Lightning Component It is used for create a html table in lightning component.
deleteNestedQueryWrapperCmpController.js JavaScript Controller File It is used for fetch the retrieve related list record using init fuction.
deleteNestedQueryWrapperCmpHelper.js JavaScript Helper File It is used for refresh the content page after deleted the record.
deleteNestedQueryWrapperCmp.css Component Style CSS It is used for create a custom delete icon from static resource.
nestedQueryWrapperCtrl.apxc Apex Class Controller It’s retrieve the data from multiple object and delete cross object records of selected row from database server

Custom Parent Object:- NewStudent__c

Custom Parent Object Fields:-

Name,

Email__c

Address__c

Custom Child Lookup Object:- Registration__c

Custom Child Object Fields:-

Name,

RegNewStudent__r.Name

RegCourse__r.Name

DateOfRegistration__c

Custom Object and their fields In this post we used two type of custom sObject. First is Parent object (NewStudent__c) and Second is child sObject (Registration__c).

Final Output β†’

delete cross object records of selected row in salesforce -- 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 Component β†’

Step 1:- Create Lightning Component : deleteNestedQueryWrapperCmp.cmp

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

deleteNestedQueryWrapperCmp.cmp [Lightning Component File]

  1.   <aura:component controller="nestedQueryWrapperCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
  2. 	<aura:attribute name="wrapListItems" type="NewStudent__c[]"/>
  3.     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
  4.     <aura:attribute name="recSelectId" type="String" default=""/>
  5.  
  6.     <div class="slds slds-p-horizontal--medium">
  7.         <table class="slds-table slds-table--bordered slds-table--col-bordered" style="border-collapse:collapse;">
  8.             <thead> 
  9.                 <tr>
  10.                     <th>Student Name</th>
  11.                     <th>Address</th>
  12.                     <th>Email</th>                    
  13.                     <th>regNum</th>
  14.                     <th>regStudentName</th>
  15.                     <th>regCourseName</th>
  16.                     <th>regDateOfReg</th>
  17.                     <th></th>
  18.  
  19.                 </tr>
  20.             </thead>
  21.             <tbody>
  22.                 <aura:iteration items="{!v.wrapListItems}" var="wrapVar" indexVar="index">
  23.                     <tr id="{!wrapVar.recStudentId}">
  24.                         <td>{!wrapVar.studentName}</td>                        
  25.                         <td>{!wrapVar.studentAddress}</td>                        
  26.                         <td>{!wrapVar.studentEmail}</td>
  27.                         <td>{!wrapVar.regNum}</td>                        
  28.                         <td>{!wrapVar.regStudentName}</td>
  29.                         <td>{!wrapVar.regCourseName}</td>
  30.                         <td>{!wrapVar.regDateOfReg}</td>
  31.  
  32.                        <td style="width:85px; "> 
  33.                                     <a href="javascript:void(0);"><span class="delSpan slds-m-left--small" title="Delete" onclick="{!c.deleteRowId}" 
  34.                                                                         data-sfid="{!wrapVar.recStudentId}"
  35.                                                                         data-index="{!index}"></span></a>
  36.  
  37.                        </td> 
  38.  
  39.                     </tr>
  40.                 </aura:iteration>
  41.             </tbody>
  42.         </table>
  43.  
  44.  <br/><br/>
  45.    <!--Start RelatedTopics Section-->
  46. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  47.  
  48.             <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>
  49.  
  50.             <br/><br/>
  51.             <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>
  52.             <div style="display:block; overflow:hidden;"> 
  53.                 <div style="width: 50%; float:left; display:inline-block">
  54.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  55.                         <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>
  56.                         <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>
  57.                         <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>
  58.                         <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>
  59.                         <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>
  60.                     </ul>
  61.             </div>
  62.  
  63.             <div style="width: 50%; float:left; display:inline-block">
  64.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  65.                         <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>
  66.                         <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>
  67.                         <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>
  68.                         <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>
  69.                         <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>
  70.                     </ul>
  71.                 </div>
  72.                <div style="clear:both;"></div> 
  73.                <br/>
  74.                 <div class="youtubeIcon">
  75.                     <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>
  76.                 </div>
  77.     </div>
  78.  
  79. </div>
  80.  
  81.   <!--End RelatedTopics Section-->
  82.  
  83.     </div>
  84. </aura:component>

Create Component JavaScript Controller β†’

Step 2:- Create Lightning Component : deleteNestedQueryWrapperCmpController.js

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

deleteNestedQueryWrapperCmpController.js [JavaScript Controller]

  1.   ({
  2. 	doInit : function(component, event, helper) {    
  3.  
  4. 	  var action = component.get("c.appCustomWrapListMthd"); 
  5.  
  6.         action.setCallback(this, function(response){
  7.             var state = response.getState();
  8.             //alert(state);
  9.             if(state=='SUCCESS'){
  10.                 var result = response.getReturnValue();
  11.               //alert('result ' + JSON.stringify(result));
  12.                 component.set('v.wrapListItems',result);
  13.             }
  14.         });
  15.         $A.enqueueAction(action);
  16.  
  17. 	},
  18.  
  19.  
  20.  
  21.     deleteRowId:function(component,event,helper){
  22.         var sfid = event.target.dataset.sfid;        
  23.         var action =component.get('c.delectRecId');       
  24.         action.setParams({'delRecId':sfid});
  25.         action.setCallback(this, function(response){
  26.             var state = response.getState();
  27.             if(state == 'SUCCESS'){
  28.                 var result = response.getReturnValue();
  29.  
  30.                 var wrapListItems = component.get( "v.wrapListItems" );
  31.                 component.set( "v.wrapListItems", wrapListItems );
  32.                 helper.refreshView(component);
  33.                 //alert('record deleted successfully');
  34.                 var eventToast = $A.get("e.force:showToast");
  35.                 eventToast.setParams({
  36.                     "title":'Success',
  37.                     "type":'success',
  38.                     "message":'Record deleted successfully'
  39.                    });
  40.                  eventToast.fire();
  41.  
  42.             }
  43.         });
  44.         $A.enqueueAction(action);
  45.     },
  46.  
  47.  
  48. })

Create Component JavaScript Helper β†’

Step 3:- Create Lightning Component : deleteNestedQueryWrapperCmpHelper.js

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

deleteNestedQueryWrapperCmpHelper.js [JavaScript Helper File]

  1.     ({
  2. 	refreshView : function(component, event, helper) {
  3. 		var action = component.get("c.appCustomWrapListMthd"); 
  4.  
  5.         action.setCallback(this, function(response){
  6.             var state = response.getState();
  7.             //alert(state);
  8.             if(state=='SUCCESS'){
  9.                 var result = response.getReturnValue();
  10.                // alert('result ' + JSON.stringify(result));
  11.                 component.set('v.wrapListItems',result);
  12.             }
  13.         });
  14.         $A.enqueueAction(action);
  15. 	}
  16. })

Create Component Style CSS β†’

Step 4:- Create Lightning Component : Component Style CSS : deleteNestedQueryWrapperCmp.css

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

Lightning Component [Component Style CSS]

  1.   .THIS {
  2. }
  3. .THIS .slds-table thead tr th{background-color:#eee;}
  4. .THIS .slds-table thead tr th, .THIS .slds-table tbody tr td {border:1px #dddbda solid; border-collapse: collapse; white-space: normal;}
  5.  
  6. .THIS .delSpan:before{content:''; width:17px; height:17px; display:inline-block; background:url(https://www.w3web.net/wp-content/uploads/2022/01/delete.png) no-repeat left top; background-size:cover; cursor: pointer;}

Create Apex Class Controller β†’

Step 5:- Create Apex Class : nestedQueryWrapperCtrl.apxc

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

nestedQueryWrapperCtrl.apxc [Apex Class Controller]

  1.    public class nestedQueryWrapperCtrl {
  2.    public class wrapperCustomClass{
  3.         @AuraEnabled
  4.         public string studentName;
  5.         @AuraEnabled
  6.         public string studentEmail;
  7.         @AuraEnabled
  8.         public string studentAddress;
  9.         @AuraEnabled
  10.         public Id recStudentId;
  11.  
  12.  
  13.         @AuraEnabled
  14.         public string regNum;
  15.         @AuraEnabled
  16.         public string regStudentName;
  17.         @AuraEnabled
  18.         public string regCourseName;
  19.         @AuraEnabled
  20.         public DATE regDateOfReg;
  21.         @AuraEnabled
  22.         public BOOLEAN editMode;
  23.  
  24.  
  25.         public wrapperCustomClass(string studentName, string studentEmail, string studentAddress, Id recStudentId,
  26.                                                        string regNum,string regStudentName,string regCourseName,DATE regDateOfReg,BOOLEAN editMode){
  27.          this.studentName = studentName;
  28.          this.studentEmail = studentEmail;
  29.          this.studentAddress = studentAddress;  
  30.          this.recStudentId = recStudentId;  
  31.  
  32.  
  33.          this.regNum = regNum;
  34.          this.regStudentName = regStudentName;
  35.          this.regCourseName = regCourseName; 
  36.          this.regDateOfReg = regDateOfReg;
  37.          this.editMode=editMode;
  38.  
  39.         }
  40.  
  41.     }
  42.  
  43.  
  44.     @AuraEnabled
  45.     public static List<wrapperCustomClass> appCustomWrapListMthd(){
  46.        List<NewStudent__c> newStudent = NEW List<NewStudent__c>();
  47.        List<Registration__c> newRegistration = NEW  List<Registration__c>();
  48.        List<wrapperCustomClass> custWrapObj = NEW  List<wrapperCustomClass>();
  49.  
  50.  
  51.        List<NewStudent__c> newStudentView =[SELECT Id, Name, Email__c, Address__c,(SELECT Id, Name, RegNewStudent__r.Name, RegCourse__r.Name, DateOfRegistration__c FROM Registrations__r) FROM NewStudent__c];
  52.         system.debug('newStudentView## ' + newStudentView);
  53.         newRegistration = [SELECT Id, Name, RegNewStudent__r.Name, RegCourse__r.Name, DateOfRegistration__c FROM Registration__c ];
  54.  
  55.         FOR(NewStudent__c studentObj:newStudentView){
  56.             FOR(Registration__c regObj:studentObj.Registrations__r){
  57.                  custWrapObj.add(NEW wrapperCustomClass(studentObj.Name,studentObj.Email__c, studentObj.Address__c,
  58.                                                   studentObj.Id,regObj.Name,regObj.RegNewStudent__r.Name,regObj.RegCourse__r.Name,regObj.DateOfRegistration__c,FALSE));
  59.             }
  60.  
  61.         }
  62.  
  63.         system.debug('custWrap@@' + custWrapObj);
  64.         RETURN custWrapObj;
  65.  
  66.     }    
  67.  
  68.  
  69.     @AuraEnabled
  70.     public static List<NewStudent__c> delectRecId(Id delRecId){
  71.         DELETE [SELECT Id, (SELECT Id FROM Registrations__r WHERE RegNewStudent__c=:delRecId) FROM NewStudent__c WHERE Id=:delRecId];       
  72.         List<NewStudent__c> listStudent = [SELECT Id, Name, Email__c, Address__c,(SELECT Id, Name, RegNewStudent__r.Name, RegCourse__r.Name, DateOfRegistration__c FROM Registrations__r) FROM NewStudent__c];
  73.         RETURN listStudent;
  74.  
  75.     }
  76.  
  77. }

delete cross object records of selected row in salesforce -- w3web.net

 

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

Which method is better for searching across multiple objects in Salesforce?

SOSL is most useful when you don't know the exact fields and objects that your data resides in, and you need to search across multiple objects.

What is SOSL in Salesforce?

Salesforce Object Search Language (SOSL) is a Salesforce search language that is used to perform text searches in records. Use SOSL to search fields across multiple standard and custom object records in Salesforce.

Can we use joins in Salesforce?

In SOQL statements, Inner and Outer Joins are used to join related objects like parent object and child objects to retrieve all related fields from base objects including the records of non refer related object.

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



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