0

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"}]

2
  • The easiest way to answer this for yourself is to use the browser inspector to inspect the content of the array returned to you from getSelectedRows. You can then write code that picks out the values you need from each row's data and use it to create a new array of IDs that you then send to your AuraEnabled function via an imperative call.
    – Phil W
    Commented Jan 28, 2020 at 12:49
  • @PhilW Thanks for the guidance. I will try doing it.
    – Koustubh
    Commented Jan 28, 2020 at 13:03

1 Answer 1

2

You can loop over the "selected" list in javascript and get the CRId of all the accounts in a list and then you can pass that list as an argument to the apex method.

See the below example-

In js file:

    let ids = selected.map(selection => selection.CRId);

    // call apex method
    del({
         deleteIds : ids
    })

In apex file:

    public static void del(Id[] deleteIds) {
        System.debug('deleteIds'+deleteIds);
        //.. do what you want
    }
2
  • 1
    Assuming it is only the "CRId" that is of interest... as the OP originally said, it isn't a homogeneous list of values that want to be deleted. (I note the update that now says just the CRId is of interest). Additionally, there's a nicer way to do what you suggested: let ids = selected.map(selection => selection.CRId);
    – Phil W
    Commented Jan 28, 2020 at 14:08
  • @PhilW and AviRai, Thanks for your answers! It helped me a lot. For edit functionality, I need to pass other fields as well. How can I send multiple fields to apex instead of just one field? I asked a question in relation to it here: salesforce.stackexchange.com/questions/293216/… Please help. Thanks again!
    – Koustubh
    Commented Feb 3, 2020 at 4:17

You must log in to answer this question.

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