0

I'm still very new to Salesforce/Apex development but I'm struggling to understand how I would pass the SelectedRows of a datatable through to Apex. I've managed to pass simple data types through on the click of a button but I'm not sure as to what the datatype of the list/array would need to be within Apex. Ideally I would still need to access not only the Id of the SelectedRow but also the data contained within the row. Any help would be greatly appreciated!!

LWC JS

handleConfirm(){
    createNewRec({stringNameType: this.stringNameType, lstSelectedRecords})
    .then(result =>{
        this.result = result;
    })
    .catch(error =>{
        this.error = error;
    })
}

Apex

@AuraEnabled
public static String createNewRec(String stringNameType, List<???>lstSelectedRecords){
    String returnResult = 'something';
    system.debug('stringNameType: ' + stringNameType + ' lstSelectedRecords ' +lstSelectedRecords);
    return returnResult;
}

1 Answer 1

2

Per documentation:

selectedRows - The data in the rows that are selected.

Standard/Custom Objects

If your datatable displays some kind of Standard or Custom object. You can pass List<YourStandardObject> or List<YourCustomObject>

@AuraEnabled
public static void createNewRecord(List<Account> selectedRecords) {
    insert selectedRecords;
}

Others

You can always pass List<Map<String, Object>> and do some mapping in Apex.

@AuraEnabled
public static void createNewRecord(List<Map<String, Object>> selectedRecords) {
    // mapping here
    // example
    List<Account> accountsToInsert = new List<Account>();
    for (Map<String, Object> fields : selectedRecords) {
         accountsToInsert.add(
            new Account(
               Name = fields.get('Name')
            )
         );
     }
    insert accountsToInsert;
}

You must log in to answer this question.

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