I have created an LWC to edit a custom object record. The LWC is wrapped around Aura component that is used to override standard edit button. Once I create a record of the custom object and open for edit, the LWC opens and works fine. Now if I go back to the custom object tab (i.e. to a list view) and delete the record that was previously opened in the LWC, the LWC tries to access the deleted record and I get error. The LWC gets recordId as API input of the record to open for edit. I tried two options,
In LWC, I have a wired call to retrieve the record using getRecord(recordId:$recordId). Once I open a record with the LWC, go back to the custom object tab and delete the record that was opened, this wired method gets called even if the LWC is not open/visible. It seems to be sitting in DOM. Because of this, I get error since the record is deleted.
In LWC, in connectedcallback, I use my own apex method to retrieve the record details and initialize it to open. Then go back to the custom object tab to delete the record. At this point it seem to be calling something that gives me "Error. NOT_FOUND". Not sure what's calling it. I do not see anything in my code that show this error.
I am wondering why it calls my LWC method when a record (that was previously opened in my LWC) is deleted? The LWC instance seems to be in the DOM. Since I have LWC displayed in an app page, I can simply click the custom object tab so looks like the LWC instance still stays in DOM. Not sure if there is a way to remove the LWC instance from DOM when user goes away from the LWC instance by clicking another tab.