Example of lightning-datatable inline to edit/save rows of records and refresh lwc component on click button in Salesforce Lightning Web Component — LWC | Inline Edit/Save Field and refresh the component after successful save of standard record page in Salesforce LWC

18,064 views

Hey guys, today in this post we are going to learn about How to Inline Edit/Save Field and refresh the component after successful save of standard record page in Salesforce LWC.

To display Salesforce data in a table, use the lightning-datatable component. The component supports inline editing, which enables users to update a field value without navigating to the record.

To load a list of records, use the getListUi (Deprecated) wire adapter or use SOQL in an Apex method. To Know more details about inline editing/save field in lwc, click Here.

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

 

Files we used to create inline editing/save field in Salesforce LWC →

lwcEditSaveRow.html LWc HTML File Template HTML file to edit/save rows inline in Salesforce Lightning Web Component (LWC)
lwcEditSaveRow.js LWC JavaScript File It’s hold a javascript on click function to Save and refresh the component after successful in lwc.
lwcEditSaveRow.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage.
lwcEditSaveRowCtrl.cls Apex Controller It is used to Fetch the Account records through @wire Decorators from database.

Final Output →

lightning datatable inline edit lwc -- 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 Web Component HTML →

Step 1:- Create Lightning Web Component : lwcEditSaveRow.html

SFDX:Lightning Web Component >> New >> lwcEditSaveRow.html

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

lwcEditSaveRow.html [Lightning Web Component HTML]

  1.    <template>
  2.     <lightning-card>
  3.         <div class="slds-m-around_medium">
  4.         <h3 class="slds-text-heading_medium"><lightning-icon icon-name="custom:custom84" size="small"></lightning-icon> <strong style="color:#270086; font-size:13px; margin-right:5px;"> How to inline Edit/Save Rows With Lightning Datatable in Lightning Web Component (LWC) </strong></h3>
  5.         <br/><br/>
  6.         <template if:true={accObj.data}>
  7.             <lightning-datatable key-field="Id" 
  8.             data={accObj.data} 
  9.             columns={columns} 
  10.             onsave={saveHandleAction}
  11.             draft-values={fldsItemValues} 
  12.             hide-checkbox-column 
  13.             show-row-number-column>
  14.            </lightning-datatable>
  15.         </template>
  16.         <br/>
  17.          <br/>
  18.    <!--Start RelatedTopics Section-->
  19. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  20.  
  21.             <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>
  22.  
  23.             <br/><br/>
  24.             <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>
  25.             <div style="display:block; overflow:hidden;"> 
  26.                 <div style="width: 50%; float:left; display:inline-block">
  27.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  28.                         <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>
  29.                         <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>
  30.                         <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>
  31.                         <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>
  32.                         <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>
  33.                     </ul>
  34.             </div>
  35.  
  36.             <div style="width: 50%; float:left; display:inline-block">
  37.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  38.                         <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>
  39.                         <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>
  40.                         <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>
  41.                         <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>
  42.                         <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>
  43.                     </ul>
  44.                 </div>
  45.                <div style="clear:both;"></div> 
  46.                <br/>
  47.                 <div class="youtubeIcon">
  48.                     <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>
  49.                 </div>
  50.     </div>
  51.  
  52. </div>
  53.  
  54.   <!--End RelatedTopics Section-->
  55.     </div>
  56.     </lightning-card>
  57. </template>

Create Lightning Web Component JavaScript →

Step 2:- Create Lightning Web Component : lwcEditSaveRow.js

SFDX:Lightning Web Component >> New >> lwcEditSaveRow.js

lwcEditSaveRow.js [LWC JavaScript File]

  1.    import { LightningElement, wire, track } from 'lwc';
  2. import getAccounts from '@salesforce/apex/lwcEditSaveRowCtrl.getAccounts';
  3. import { updateRecord } from 'lightning/uiRecordApi';
  4. import { ShowToastEvent } from 'lightning/platformShowToastEvent';
  5. import { refreshApex } from '@salesforce/apex';
  6.  
  7. const columns = [
  8.     {
  9.         label: 'Name',
  10.         fieldName: 'Name',
  11.         type: 'text',
  12.     }, {
  13.         label: 'Phone',
  14.         fieldName: 'Phone',
  15.         type: 'phone',
  16.         editable: true,
  17.     }, {
  18.         label: 'Industry',
  19.         fieldName: 'Industry',
  20.         type: 'text',
  21.         editable: true,
  22.     }, {
  23.         label: 'Type',
  24.         fieldName: 'Type',
  25.         type: 'text',
  26.         editable: true
  27.     }, {
  28.         label: 'Description',
  29.         fieldName: 'Type',
  30.         type: 'text',
  31.         editable: true
  32.     }
  33.  
  34. ];
  35. export default class LwcEditSaveRow extends LightningElement {
  36.     columns = columns;
  37.     @track accObj;
  38.     fldsItemValues = [];
  39.  
  40.     @wire(getAccounts)
  41.     cons(result) {
  42.         this.accObj = result;
  43.         if (result.error) {
  44.             this.accObj = undefined;
  45.         }
  46.     };
  47.  
  48.     saveHandleAction(event) {
  49.         this.fldsItemValues = event.detail.draftValues;
  50.         const inputsItems = this.fldsItemValues.slice().map(draft => {
  51.             const fields = Object.assign({}, draft);
  52.             return { fields };
  53.         });
  54.  
  55.  
  56.         const promises = inputsItems.map(recordInput => updateRecord(recordInput));
  57.         Promise.all(promises).then(res => {
  58.             this.dispatchEvent(
  59.                 new ShowToastEvent({
  60.                     title: 'Success',
  61.                     message: 'Records Updated Successfully!!',
  62.                     variant: 'success'
  63.                 })
  64.             );
  65.             this.fldsItemValues = [];
  66.             return this.refresh();
  67.         }).catch(error => {
  68.             this.dispatchEvent(
  69.                 new ShowToastEvent({
  70.                     title: 'Error',
  71.                     message: 'An Error Occured!!',
  72.                     variant: 'error'
  73.                 })
  74.             );
  75.         }).finally(() => {
  76.             this.fldsItemValues = [];
  77.         });
  78.     }
  79.  
  80.  
  81.     async refresh() {
  82.         await refreshApex(this.accObj);
  83.     }
  84. }

Create Lightning Web Component Meta XML →

Step 3:- Create Lightning Web Component : lwcEditSaveRow.js-meta.xml

SFDX:Lightning Web Component >> New >> lwcEditSaveRow.js-meta.xml

lwcEditSaveRow.js-meta.xml [LWC Meta Data XML]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets> 
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.     </targets>
  10. </LightningComponentBundle>

Create Apex Class Controller →

Step 4:- Create Apex Class : lwcEditSaveRowCtrl.cls

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

lwcEditSaveRowCtrl.cls [Apex Class Controller]

  1.   public WITH sharing class lwcEditSaveRowCtrl {
  2.  
  3.     @AuraEnabled(Cacheable = TRUE)
  4.     public static List<Account> getAccounts() {
  5.  
  6.         List<Account> accList = [SELECT Id, Name, Phone, Industry, TYPE, Website, Description FROM Account WHERE Phone != NULL AND Industry != NULL ORDER BY CreatedDate DESC ];
  7.  
  8.         RETURN accList;
  9.     }    
  10. }

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

What is inline editing in Salesforce lightning?

Inline editing is a feature by which we can edit a record without pressing the edit button. We can go to the detail page, doubles click on a field, changes to a new value and click on Save and field value is updated to the new value.

Why is my inline edit disabled Salesforce?

The Inline Editing feature may be disabled for List Views when there are more than five (5) Lines of Filter Criteria or when you add Filter Logic. Reduce your Filter Criteria and/or remove Filter Logic, save your List View and try Inline Editing again.

Which fields are not editable with the inline editing in list view?

Full Name fields on Person Accounts, Contacts, Leads, and Opportunities aren't inline editable; however, their component fields are, such as First Name and Last Name. Take note that this also includes the record owner field.

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