How to you make Edit/Save records of Account on edit button click as custom functionality using apex class controller in lightning component Salesforce | How to update/edit Account records uses of apex class in lightning component Salesforce

4,440 views

Hey guys, today in this post we are going to learn about How to you make Edit/Save records of Account on edit button click as custom functionality in lightning component Salesforce.

To edit a record using lightning:recordForm, provide the record ID and object API name. When you provide a record ID, view mode is the default mode of this component, which displays fields with edit icons. If you click an edit icon, all fields in the form become editable. To know more details, Click Here →

 

Files we used to create Edit/Save Functionality in aura:component →

editRecordCmp.cmp Lightning Component It is used to create a edit/save records form in lightning component
editRecordCmpController.js JavaScript Controller It is hold Javascript Edit/Save functionality in lightning component.
editRecordCtrl.apxc Apex Class Controller It is used for get SOQL query to retrieve data from Apex Method
editRecordApp.app Lightning Application It is used to call the component and preview on browser.

 

 

Final Output →

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

 

how to edit update records in lightning -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

 

  • Find the below steps ▾

 

Create Lightning Component

Step 1:- Create Lightning Component : editRecordCmp.cmp

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

editRecordCmp.cmp [Lightning Component File]

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.      <aura:component controller="editRecordCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
  2.     <aura:attribute name="acclist" type="list"/>
  3.     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
  4.     <aura:attribute name="editData" type="boolean"/>
  5.  
  6.     <div class="slds slds-p-around_large"> 
  7.         <h2 class="slds-text-heading_medium"><lightning:icon iconName="custom:custom86" size="small"/> <strong style="color:#270086; font-size:16px; margin-right:5px;"> How to Edit/Update Records in Lightning Component [<a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer">View more articles →</a>]</strong></h2>
  8.         <br/>
  9.         <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered" style="border:1px #ddd solid;">
  10.             <thead> 
  11.                 <tr>
  12.                     <td colspan="4">
  13.                         <div style="display:block; text-align:center;">
  14.                             <aura:if isTrue="{!!v.editData}">
  15.                                 <lightning:button label="Edit" variant="brand" onclick="{!c.dataEditAction}"/>
  16.                             </aura:if>
  17.  
  18.                             <aura:if isTrue="{!v.editData}">
  19.                                 <lightning:button variant="brand" label="Save" onclick="{!c.dataSaveAction}"/>
  20.                                 <lightning:button variant="destructive" label="Cancel" onclick="{!c.dataCancelAction}"/>
  21.                             </aura:if>
  22.                         </div>
  23.                     </td>
  24.                 </tr>
  25.  
  26.                 <tr>
  27.                     <th style="background:#ddd;">Name</th>
  28.                     <th style="background:#ddd;">Phone</th>
  29.                     <th style="background:#ddd;">Industry</th>
  30.                     <th style="background:#ddd;">Type</th>
  31.                 </tr>
  32.             </thead>
  33.             <tbody>
  34.                 <aura:if isTrue="{!!v.editData}">
  35.                     <aura:iteration items="{!v.acclist}" var="Items">
  36.                         <tr>
  37.                             <td>{!Items.Name}</td>
  38.                             <td>{!Items.Phone}</td>
  39.                             <td>{!Items.Industry}</td>
  40.                             <td>{!Items.Type}</td>
  41.                         </tr>
  42.                     </aura:iteration>
  43.                 </aura:if>
  44.                 <aura:if isTrue="{!v.editData}">
  45.                     <aura:iteration items="{!v.acclist}" var="Items">
  46.                         <tr>
  47.                             <td>
  48.                                 <lightning:input value="{!Items.Name}"/>
  49.                             </td>
  50.                             <td>
  51.                                 <lightning:input value="{!Items.Phone}"/>
  52.                             </td>
  53.                             <td>
  54.                                 <lightning:input value="{!Items.Industry}"/>
  55.                             </td>
  56.                             <td>
  57.                                 <lightning:input value="{!Items.Type}"/>
  58.                             </td>
  59.                         </tr>
  60.                     </aura:iteration>
  61.                 </aura:if>
  62.  
  63.                 <tr>
  64.                     <td colspan="4">
  65.                         <div style="display:block; text-align:center;">
  66.                             <aura:if isTrue="{!!v.editData}">
  67.                                 <lightning:button label="Edit" variant="brand" onclick="{!c.dataEditAction}"/>
  68.                             </aura:if>
  69.  
  70.                             <aura:if isTrue="{!v.editData}">
  71.                                 <lightning:button variant="brand" label="Save" onclick="{!c.dataSaveAction}"/>
  72.                                 <lightning:button variant="destructive" label="Cancel" onclick="{!c.dataCancelAction}"/>
  73.                             </aura:if>
  74.                         </div>
  75.                     </td>
  76.                 </tr> 
  77.             </tbody>  
  78.  
  79.         </table>
  80.  
  81.  
  82.  
  83.         <!--Start RelatedTopics Section-->
  84.         <br/><br/>
  85. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  86.  
  87.     <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;"/><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>
  88.  
  89.             <br/><br/>
  90.         <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;"/><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>
  91.             <div style="display:block; overflow:hidden;"> 
  92.                 <div style="width: 50%; float:left; display:inline-block">
  93.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  94.                         <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>
  95.                         <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>
  96.                         <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>
  97.                         <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>
  98.                         <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>
  99.                     </ul>
  100.             </div>
  101.  
  102.             <div style="width: 50%; float:left; display:inline-block">
  103.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  104.                         <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>
  105.                         <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>
  106.                         <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>
  107.                         <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>
  108.                         <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>
  109.                     </ul>
  110.                 </div>
  111.                <div style="clear:both;"></div> 
  112.                <br/>
  113.                 <div class="youtubeIcon">
  114.                     <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>
  115.                 </div>
  116.     </div> 
  117. </div>
  118.  
  119.   <!--End RelatedTopics Section-->
  120.  
  121.     </div>
  122. </aura:component>

 

Create JavaScript Controller

Step 2:- Create Lightning Component : editRecordCmpController.js

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

editRecordCmpController.js [JavaScript Controller]

  1.    ({
  2.  
  3.     //doInit function to fetch the data
  4. 	doInit : function(component, event, helper)
  5.     {
  6.         component.set("v.editData",false);
  7.         var action=component.get("c.getAccountList");
  8.         action.setCallback(this,function(response){
  9.             var state=response.getState();           
  10.             if(state==='SUCCESS')            {
  11.  
  12.                 var result=response.getReturnValue();
  13.                 component.set("v.acclist",result);
  14.             }
  15.             else
  16.             {
  17.                 console.log('Some Error occurred during fetching the data');
  18.             }
  19.  
  20.         });
  21.         $A.enqueueAction(action);
  22.  
  23.  },
  24.  
  25.     //dataEditAction function to edit the data
  26.  
  27.     dataEditAction:function(component,event,helper)
  28.     {
  29.         component.set("v.editData",true);
  30.     },
  31.  
  32.  
  33.      //dataSaveAction function to Save the data
  34.     dataSaveAction:function(component,event,helper)
  35.     {
  36.  
  37.         component.set("v.editData",false);
  38.         var action=component.get("c.updateAccRecords");
  39.         action.setParams({
  40.             accdata:component.get("v.acclist")
  41.  
  42.         });
  43.           action.setCallback(this,function(response){
  44.             var state=response.getState();
  45.  
  46.             if(state==='SUCCESS')
  47.             {               
  48.                 var result=response.getReturnValue();
  49.                 component.set("v.acclist",result);
  50.  
  51.                 var toastEvent = $A.get("e.force:showToast");
  52.                 toastEvent.setParams({
  53.                     "title": "Success!",
  54.                     "type":'success',
  55.                     "message": "The record has been updated successfully."
  56.                 });
  57.                 toastEvent.fire();
  58.  
  59.             }
  60.             else
  61.             {
  62.                 console.log('Some Error occurred during fetching the data');
  63.             }
  64.  
  65.         });
  66.         $A.enqueueAction(action);
  67.     },
  68.  
  69.  
  70.     //dataCancelAction function to Cancel the data
  71.     dataCancelAction:function(component,event,helper)
  72.     {
  73.         component.set("v.editData",false);
  74.     }
  75.  
  76. })

 

Create Apex Class Controller

Step 3:- Create Apex Class : editRecordCtrl.apxc

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

editRecordCtrl.apxc [Apex Class Controller]

  1.    public class editRecordCtrl {
  2.  
  3.     @AuraEnabled
  4.       public static List<Account> getAccountList(){
  5.           List<Account> accList = [SELECT Id, Name, Phone, Industry, TYPE FROM Account WHERE Industry !=NULL LIMIT 10];
  6.           RETURN accList;
  7.       }
  8.  
  9.     @AuraEnabled
  10.     public static List<Account> updateAccRecords(List<Account> accdata)
  11.     {
  12.         try
  13.         {
  14.             UPDATE accdata;
  15.         }
  16.         catch(Exception e)
  17.         {
  18.             System.debug('unable to update the record due to '+e.getMessage());
  19.         }
  20.         RETURN accdata;
  21.     }
  22. }

 

Create Lightning Application

Step 4:- Create Lightning Application : editRecordApp.app

From Developer Console >> File >> New >> Lightning Application

editRecordApp.app [Component Application File]

  1.        <aura:application extends="force:slds">
  2.     <c:editRecordCmp/>
  3. </aura:application>

 
how to edit update records in lightning -- w3web.net
 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

How do you make a field editable in lightning component?

To specify editable fields, use lightning-input-field components inside lightning-record-edit-form component. See the Editing a Record section. To display record fields as read-only in lightning-record-edit-form , use lightning-output-field components to specify those fields.

Can we edit lightning components in production?

Lightning Components can be changed in Production, however it is not recommended because it would cause discrepancies between any and all Sandboxes that were created before the change on Production.

How do I edit a custom lightning component?

Custom Lightning components configured for Experience Builder sites behave just like standard template components. Drag the component to the page canvas. To edit its properties, select the component on the page canvas, and then enter changes in the floating component property editor.

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