Renaming a Document on_submit of a Sales Invoice causes an "Document can't be found" error

The frappe.desk.notifications.get_open_count method still seems to be referencing the old doc name even though I have already successfully changed the doc name from SINV-00083 to ADCSINV-00084.

This causes the error message to popup.

Anyone has any ideas on what I should do after successfully changing the name from the script POV?

A quick primer on my code. Basically what I’m trying to do is to create a mirroring doc in a custom docType called “Sales Invoice Number”, so that I can tap on the default naming_series logic there, to get a document number ONLY on submit of my “Sales Invoice”. So therefore my Sales Invoice is overwritten by the name from Sales Invoice Number. This is because there is a current limitation in ERPNext where a running number is generated even when the document is in draft, which creates issues in some countries because of the number “holes” left behind if a Draft Doc is deleted.

Everything seems to work fine, just wrecking my brains on how to get rid of the error message. I tried frappe.model.remove_from_locals(“Sales Invoice”, frm.doc.name); but to no avail. Have already spent countless frustrating days on this.

Was hoping the community can help with this.

Thanks in advance!!

I attached a video as well so that it would be easier to visualize what I’m talking about.
https://drive.google.com/file/d/1_bltsbQ3lidoIEUOBn3eSJYl-uV6iBWw/view?usp=sharing

frappe.ui.form.on('Sales Invoice', "before_submit", function(frm,cdt, cdn) 
{
    var d = locals[cdt][cdn];

frappe.db.insert(
    {
    "doctype": "Sales Invoice Number",
    "sales_doc_name": d.name
    }).then(function(doc) 
    {
        console.log(doc);
    }
    );
});


frappe.ui.form.on('Sales Invoice', "before_submit", function(frm,cdt, cdn) 
{
    var d = locals[cdt][cdn];
 {
     setTimeout(()=>{frappe.call({
            method: 'frappe.client.get_value',
            args: {
                'doctype': 'Sales Invoice Number',
                'filters': {'sales_doc_name': frm.doc.name},
                'fieldname': ['name']
            },
            callback: function(r) {
                frappe.set_route("Form", "Sales Invoice", r.message.name);
                if (r.message) {
                    
                    frappe.call({
                    method: 'frappe.client.rename_doc',
                    type: 'POST',
                    args: 
                    {
                     
                          "doctype": "Sales Invoice",
                          "old_name": frm.doc.name,
                          "new_name": r.message.name,
                          "rebuild_search": true
                    }
                    });
                    setTimeout(()=>{},2000);
                } 
            }
        });
        },500);
        
         setTimeout(()=>
            {
             window.location.reload();
            },3500);
	}
});

frappe.ui.form.on('Sales Invoice',  
{
    on_submit: function(frm) 
    {
       setInterval(()=>{
           frappe.show_progress('Loading....<br><br>We are currently updating all the necessary...<br><br>', 70, 100, 'Please wait.');
       },100);
      
    }
});