I get data from a wrapper class and flatten the data in JS file of my LWC as follows:
@wire(getAccount, { id: '$recordId' }) wired(result) {
this.refreshTable = result;
if (result.data) {
//create an array
let preparedAccounts = [];
//use for each loop and put one row at a time in variable named account
result.data.forEach(account => {
//create a row and add data
let preparedAccount = {};
preparedAccount.Account_Id = account.AccountId.Id;
preparedAccount.Contact_Id = account.ContactID.Id;
preparedAccount.CRId = account.CRId;
//push row into array
preparedAccounts.push(preparedAccount);
});
//assign array to @track accountsData variable
this.accountsData = preparedAccounts;
}
}
More detail about flattening data
I want to use this data to delete custom object records. For this, I need to pass the data to the apex class. I do it as follows:
deleteRelationship() {
// Gets data of selected rows
let selected = this.template.querySelector('lightning-datatable').getSelectedRows();
//get data of all the selected rows of datatable
// calling apex class method to delete the selected contact
if (selected.length > 0) {
del({ deleteIds: selected })
.then(result => {
window.console.log('result ====> ' + result);
})
.catch(error => {
window.console.log('result ====> ' + error);
});
}
}
How do I write the signature of the apex class to accept the data coming from JS file? I tried the following ways but they fail.
@AuraEnabled
public static void del(Map<String,String> deleteIds) {
//gives null value for deleteIds
}
@AuraEnabled
public static void del(String[] deleteIds) {
//gives error "Value provided is invalid for action parameter 'deleteIds' of type 'List'"
}
The data that I am trying to pass to apex function in not of a pirticular object. It's just flattened data. How do I pass it to apex? Please help. Thanks!
Update:
The data returned by getSelectedRows(). I just want to send CRId
field value to the apex class and not the whole data.
Selected:[{"Contact_Role":"User Contact", "Account_Id":"0015400000Nmy5PAAR", "Contact_Id":"0035400000LHBGLAA5", "Account_Name":"Yahoo Inc Taiwan", "Account_MDM_Id":"10009899", "Is_Primary":false, "CRId":"a0854000002jBoRAAU"}]