0

I have a datatable in an LWC where rows can be selected for deletion. The delete process is done via a batch job due to multiple triggers running for the object being deleted. Once I've kicked off the batch job though, I'd like to wait to refresh the LWC datatable to show the results as confirmation of the delete.

So deleteRecs below calls a batch apex job. How do I make it wait before doing the toast and refreshApex?

deleteSelected() {
    var idlist = this.selectedRecs.map((row) => {
      return row.Id;
    });
    this.selectedCount = this.selectedLen;
    deleteRecs({ ids: idlist, client: this.selClient })
      .then(() => {
        this.dispatchEvent(
          new ShowToastEvent({
            title: "Successful deletion",
            message: "Successfully deleted " + this.selectedCount + " records",
            variant: "success"
          })
        );
        this.searchstr = "";
        refreshApex(this.wiredRecs);
      })
      .catch((error) => {
        this.dispatchEvent(
          new ShowToastEvent({
            title: "Error on delete",
            message: error.body.message,
            variant: "error",
            mode: "sticky"
          })
        );
      });
    this.template.querySelector("lightning-datatable").selectedRows = [];
    this.selectedRecs = undefined;
}

1 Answer 1

2

Batch jobs run asynchronously, so you need a different mechanism. Consider using a platform event so the client can be notified when the job is done. On the client, use lightning-emp-api to subscribe to an event before you start the job, then use EventBus.publish in your finish method to deliver notification that the job completed. You can use custom fields to make sure that the client knows which batch finished (use the Id from Database.executeBatch).

You must log in to answer this question.

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