0

I am trying to update value of a particular field that is Status from "In Progress" to "Submitted" when checkbox for that particular row is selected and "Submit" button is clicked in data table. This is what I'm trying to do in Submit button onclick:

submitDataPoc() {
      var selectedRecords =  
      this.template.querySelector("lightning-datatable").getSelectedRows();
      console.log("Result1: " +selectedRecords);
      var chkStatus = JSON.stringify(selectedRecords)
      console.log("Result2: " +chkStatus); 
}

When I click the checkbox and press submit button, in console I'm getting this as response: enter image description here

Now I want to change the value of Status__c from "In Progress" to "Submitted", but not sure how to do that. Please guide me if possible.

My html code: enter image description here

3 Answers 3

2

First, I recommend using field and object tokens:

import STATUS_FIELD from '@salesforce/schema/MyObject__c.Status__c';
import OBJECT_NAAME from '@salesforce/schema/MyObject__c';

You'll also want to write an Apex method to accept this data:

@AuraEnabled public static void updateRecords(MyObject__c[] records) {
  update records;
}

Now, we'll import this method in your controller:

import updateRecords from '@salesforce/apex/MyClass.updateRecords';

From here, you just need to map the data and send it to the server:

async submitDataPoc() {
  let records = this.template.querySelector('lightning-datatable')
    .getSelectedRows().map( // Converts the Array to desired field updates
      (record) => ({
        ...record, 
        [STATUS_FIELD.fieldApiName]: 'Submitted', 
        sobjectType: OBJECT_NAME.objectApiName 
      })
    );
  await update({ records });
}
0

Try to do something like that:

orderList must be marked with @track so table values will be rerendered.

@track orderList = [];

//...
//...

submitDataPoc() {
    const selectedRows = this.template.querySelector("lightning-datatable").getSelectedRows();
    const selectedRowsNames = selectedRows.map(row => row.Name);
    this.orderList
       .filter(order => selectedRowsNames.includes(order.Name))
       .forEach(order => { order.Status__c = 'Submitted' });
}

0

I would suggest you to use Lightning data service's updateRecord(recordInput, clientOptions), if you have the id of the record to update and the fields to update. You can refer the developer guide on this api - https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_update_record

Simple to use and doesn't require any apex code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .