How to get the name of newly created doc in script

I have a client script that creates a new doc on a button click, and I want to route the user to the new doc after creation. Everything works as expected except the routing.
My problem is that the document does not have the correct name in the script.

var BOM_items = frm.doc.linked_equipment;

        var ticket = frappe.model.get_new_doc("QR Ticket");
        ticket.qr_code = frm.doc.name;
        console.log("Ticket Name: " + ticket.name);
        
        //loop through each item in linked_equipment
        BOM_items.forEach(function(entry){
            for(let i = 0; i < num_sel; i++){
                console.log("Comparing " + entry.name + " to " + selected.linked_equipment[i]);
                
                // if a match is found
                if (entry.name == selected.linked_equipment[i]){
                  console.log("     Match found!");
                    // set status of item to faulty to be pulled from QR Ticket on creation
                    entry.status = "Faulty";
                    console.log(entry.status);
                    
                }
            }
            frm.refresh_field('linked_equipment');
        });

        //save current form with updated status fields
        console.log(BOM_items[0].status);
        cur_frm.save();
        
        console.log(ticket);
        
        console.log(BOM_items[0].status);
        //insert created ticket into database
        frappe.db.insert(ticket);
        console.log(ticket.name);

The last console.log() prints the new doc’s name after it is inserted into the database, but I get “new-qr-ticket-1” instead of the actual name in the database (I can navigate to the new doc manually after creation, so it is created correctly and exists with another autogenerated name). And that means the user gets routed to a new empty doc instead of the document created in the script.

I assume I have to fetch the latest created doc of that type from the database and maybe filter it using a field, but I can’t find any way to do this on the client side.

I feel there should be a way to get the new doc’s name directly since it is created in the client script, any ideas?

Thanks,
Olle

frappe.db.insert(ticket).then( (doc) =>{console.log(doc.name);})

Thank you! This works perfectly!