You need to sign in to do that
Don't have an account?
Pooja Nekkalapudi
How to update selected records in LWC by passing selectedRow IDs into a button that updates status field on each record
LWC.js
getSelectedRec() { var selectedRecords = this.template.querySelector("lightning-datatable").getSelectedRows(); if(selectedRecords.length > 0){ console.log('selectedRecords are ', selectedRecords); let ids = ''; selectedRecords.forEach(currentItem => { ids = ids + ',' + currentItem.Id; }); this.selectedIds = ids.replace(/^,/, ''); this.lstSelectedRecords = selectedRecords; alert(this.selectedIds); } } async handleChange() { let records = this.template.querySelector('lightning-datatable') .getSelectedRows().map( // Converts the Array to desired field updates (record) => ({ ...record, [STATUS_FIELD.Role__c]: 'Actively Prospecting', sobjectType: OBJECT_NAME.Coverage_Team__c }) );
await updateRecords({ records }); }
LWC.html
<lightning-button variant="Neutral"
label="Selected Records"
title="Selected Records"
onclick={getSelectedRec}
slot="actions"
icon-name="utility:check">
</lightning-button>
<lightning-button variant="Neutral"
label="Update Records"
title="Update Records"
onclick={handleChange}
slot="actions">
</lightning-button>
<div style="border-top: 1px solid rgb(221 219 218);">
<!-- datatable -->
<lightning-datatable
key-field="id"
data={data}
columns={columns}
column-widths-mode="fixed">
</lightning-datatable>
<!-- /datatable -->
</div>
Apex controller:
public with sharing class PA_WBTeam { @AuraEnabled (cacheable=true) public static List<Coverage_Team__c> getTeam(){ return [SELECT Actively_Prospecting_Team__c,Company__c,Contact_Status__c,Employee__c,Id,Location__c,My_Coverage_Team__c,Role__c,Sponsors__c,Status__c,Website__c FROM Coverage_Team__c WHERE Role__c='Suggested by ML Model' AND My_Coverage_Team__c = 'true' ]; }
@AuraEnabled public static void updateRecords(Coverage_Team__c[] records) { update records; } }
getSelectedRec() { var selectedRecords = this.template.querySelector("lightning-datatable").getSelectedRows(); if(selectedRecords.length > 0){ console.log('selectedRecords are ', selectedRecords); let ids = ''; selectedRecords.forEach(currentItem => { ids = ids + ',' + currentItem.Id; }); this.selectedIds = ids.replace(/^,/, ''); this.lstSelectedRecords = selectedRecords; alert(this.selectedIds); } } async handleChange() { let records = this.template.querySelector('lightning-datatable') .getSelectedRows().map( // Converts the Array to desired field updates (record) => ({ ...record, [STATUS_FIELD.Role__c]: 'Actively Prospecting', sobjectType: OBJECT_NAME.Coverage_Team__c }) );
await updateRecords({ records }); }
LWC.html
<lightning-button variant="Neutral"
label="Selected Records"
title="Selected Records"
onclick={getSelectedRec}
slot="actions"
icon-name="utility:check">
</lightning-button>
<lightning-button variant="Neutral"
label="Update Records"
title="Update Records"
onclick={handleChange}
slot="actions">
</lightning-button>
<div style="border-top: 1px solid rgb(221 219 218);">
<!-- datatable -->
<lightning-datatable
key-field="id"
data={data}
columns={columns}
column-widths-mode="fixed">
</lightning-datatable>
<!-- /datatable -->
</div>
Apex controller:
public with sharing class PA_WBTeam { @AuraEnabled (cacheable=true) public static List<Coverage_Team__c> getTeam(){ return [SELECT Actively_Prospecting_Team__c,Company__c,Contact_Status__c,Employee__c,Id,Location__c,My_Coverage_Team__c,Role__c,Sponsors__c,Status__c,Website__c FROM Coverage_Team__c WHERE Role__c='Suggested by ML Model' AND My_Coverage_Team__c = 'true' ]; }
@AuraEnabled public static void updateRecords(Coverage_Team__c[] records) { update records; } }
Define a method called getSelectedRec() in your LWC.js file that will retrieve the selected records from the lightning-datatable component and store them in a variable called selectedRecords.
In the getSelectedRec() method, create a comma-separated string of the IDs of the selected records and store it in a variable called selectedIds.
Define a method called handleChange() in your LWC.js file that will update the status field of the selected records.
In the handleChange() method, use the getSelectedRows() method of the lightning-datatable component to retrieve the selected records and map them to an array of desired field updates.
Call an Apex method called updateRecords() and pass the array of field updates as a parameter.
In the Apex controller, define a method called updateRecords() that accepts an array of Coverage_Team__c records and updates them using the update DML operation.
Create a lightning-button component in your LWC.html file that calls the getSelectedRec() method when clicked to retrieve the selected records.
Create another lightning-button component in your LWC.html file that calls the handleChange() method when clicked to update the status field of the selected records.
Finally, add a lightning-datatable component to your LWC.html file to display the records, and bind its data and columns properties to the appropriate variables in your LWC.js file. (https://www.mymilestonecard.ltd/)
I came across some reference articles that would help you with your requirement -> https://developer.salesforce.com/forums/?id=9062I000000IICYQA4
https://techdicer.com/get-selected-rows-in-lightning-datatable-in-lwc/
Hope the above information helps !
Thank you.