How to return data from multiple objects using wrapper class in lightning component | wrapper class instances in lightning component to return data from multiple objects and delete selected row on click delete button in Salesforce

3,985 views


Hey guys, In this post we are going to learn about how to write inner query in Salesforce. And how to fetch data from multiple custom sObject using wrapper apex class in lightning component.

Also in this example we will achieving that how to delete child record if child record has a lookup relationship to the parent sObject.

 

Files we used in this post example β†’

wrapperCustomObjCmp.cmp Lightning Component It is used for display the Input filed of name, address, email, and button Icon of delete on the table.
wrapperCustomObjCmpConroller.js JavaScript Controller File It is used for communicate to server side apex method and fetch the record using init fuction.
wrapperCustomObjCmpHelper.js JavaScript Helper File It is used for refresh the page after delete the record.
wrapperCustomObjCmp.CSS Component Style CSS It is used for creat a custom delete icon from resource.
wrapperCustomObjCtrCmp.apxc Apex Class Controller It is used for delete and fetching the records from multiple sObject 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 another is child sObject (Registration__c). We are achieving that how to fetch records from two custom objects and how to delete child record in Lookup relationship..

Final Output β†’

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

fetch data from multiple object 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 Component β†’

Step 1:- Create Lightning Component : wrapperCustomObjCmp.cmp

From Developer Console >> File >> New >> Lightning Component >> wrapperCustomObjCmp.cmp

wrapperCustomObjCmp.cmp [Lightning Component File]

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

Create JavaScript Controller β†’

Step 2:- Create Lightning Component : wrapperCustomObjCmpConroller.js

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

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

wrapperCustomObjCmpConroller.js [JavaScript Controller]

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

Create JavaScript Helper β†’

Step 3:- Create Lightning Component : wrapperCustomObjCmpHelper.js

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

wrapperCustomObjCmpHelper.js [JavaScript Helper File]

Fetch record from multiple object by w3web.net

  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 Style CSS: wrapperCustomObjCmp.CSS

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

wrapperCustomObjCmp.CSS [JavaScript Helper File]

Note:-Static resource custom delete icon

You need to upload a custom delete icon on resource file.

  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 : wrapperCustomObjCtrCmp.apxc

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

wrapperCustomObjCtrCmp.apxc [Apex Class Controller]

  1.    public class wrapperCustomObjCtrCmp {    
  2.  
  3.     public class wrapperCustomClass{
  4.         @AuraEnabled
  5.         public string studentName;
  6.         @AuraEnabled
  7.         public string studentEmail;
  8.         @AuraEnabled
  9.         public string studentAddress;
  10.         @AuraEnabled
  11.         public Id recStudentId;       
  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.  
  22.         public wrapperCustomClass(string studentName, string studentEmail, string studentAddress, Id recStudentId,
  23.                                                        string regNum,string regStudentName,string regCourseName,DATE regDateOfReg){
  24.          this.studentName = studentName;
  25.          this.studentEmail = studentEmail;
  26.          this.studentAddress = studentAddress;  
  27.          this.recStudentId = recStudentId;           
  28.  
  29.          this.regNum = regNum;
  30.          this.regStudentName = regStudentName;
  31.          this.regCourseName = regCourseName; 
  32.          this.regDateOfReg = regDateOfReg;         
  33.  
  34.         }
  35.  
  36.     }
  37.  
  38.  
  39.     @AuraEnabled
  40.     public static List<wrapperCustomClass> appCustomWrapListMthd(){
  41.        List<NewStudent__c> newStudent = NEW List<NewStudent__c>();
  42.        List<Registration__c> newRegistration = NEW  List<Registration__c>();
  43.        List<wrapperCustomClass> custWrapObj = NEW  List<wrapperCustomClass>();    
  44.  
  45.        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];
  46.  
  47.         newRegistration = [SELECT Id, Name, RegNewStudent__r.Name, RegCourse__r.Name, DateOfRegistration__c FROM Registration__c ];
  48.  
  49.         FOR(NewStudent__c studentObj:newStudentView){
  50.  
  51.             FOR(Registration__c regObj:studentObj.Registrations__r){
  52.                  custWrapObj.add(NEW wrapperCustomClass(studentObj.Name,studentObj.Email__c, studentObj.Address__c,
  53.                                                   studentObj.Id,regObj.Name,regObj.RegNewStudent__r.Name,regObj.RegCourse__r.Name,regObj.DateOfRegistration__c));
  54.             }
  55.  
  56.         }
  57.         RETURN custWrapObj;
  58.     }
  59.  
  60.  
  61.     @AuraEnabled
  62.     public static List<NewStudent__c> delectRecId(Id delRecId){
  63.         DELETE [SELECT Id, (SELECT Id FROM Registrations__r WHERE RegNewStudent__c=:delRecId) FROM NewStudent__c WHERE Id=:delRecId];
  64.         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];
  65.         RETURN listStudent;
  66.     }
  67.  
  68. }

 

 

Further post that would you like to learn in Salesforce

 
 

 

 

 

FAQ (Frequently Asked Questions)

What are the different objects in Salesforce?

Salesforce supports several different types of objects. There are standard objects, custom objects, external objects, platform events, and BigObjects.

How many objects are there in Salesforce?

There are three kinds of Salesforce objects. Standard Objects βˆ’ The objects already created for you by the Salesforce platform. Custom Objects βˆ’ These are the objects created by you based on your business processes. External Objects βˆ’ The objects which you create map to the data stored outside your organization.

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. SOSL is similar to Apache Lucene.

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