3

I am using deleteRecord adapter from lightning/uiRecordApi. It works fine when I give it a record Id, but a would like to give it a list of records id to delete all of them at once. What I have done is use a for loop and loop through the list of ids. The problem is that Async-Await does not work with loops. Is there a way to pass a list of records Ids to deleteRecord?

DeleteSelect(){
    for(let i = 0; i < this.ListIdRecordToDelete.length; i++){
        deleteRecord(this.ListIdRecordToDelete[i].Id); 
    }
    refreshApex(this.wiredListResult); 
}
3

The official response is something I don't particularly like, because I would except (in the future) an @wire module that handles collections.

Until then, this is the officially documented stance gleaned from (not surprisingly) lightning-datatable documentation buried in the developer docs:

handleSave(event) {
    const recordInputs =  event.detail.draftValues.slice().map(draft => {
        const fields = Object.assign({}, draft);
        return { fields };
    });

    const promises = recordInputs.map(recordInput => updateRecord(recordInput));
    Promise.all(promises).then(contacts => {
        this.dispatchEvent(
            new ShowToastEvent({
                title: 'Success',
                message: 'Contacts updated',
                variant: 'success'
            })
        );
         // Clear all draft values
         this.draftValues = [];

         // Display fresh data in the datatable
         return refreshApex(this.contact);
    }).catch(error => { // Handle error });
}

So because deleteRecord should theoretically operate the same way (return promises) it should also be able to be wrapped in a similar function.

I assume here, @wire must batch or box-car the actual serverside request otherwise I dont see why they would put this in their own documentation!

  • Thank you very much I will try it as this. – Mehdi Belcadi Apr 26 at 23:26
  • @MehdiBelcadi How did it go? – Atlas Can Apr 30 at 6:53
1

I believe there is no way to mass delete using api. You have to write an apex code to delete the list of records.

Here is the reference document to call an apex method: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex

1

As noted in other answers, deleteRecord(recordId: string) accepts only a single record Id. You can handle them together using Promise.all() to handle various scenarios (eg reject when first delete fails, let all complete before handling any resolve/rejects). If you need all-or-nothing semantics then you need to do it in Apex.

It's on the backlog (#safeharbor) to add batch support to UI API as well as Lightning Data Service's @wire adapters. That involves including all-or-nothing and other configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.