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 =;
        console.log("Ticket Name: " +;
        //loop through each item in linked_equipment
            for(let i = 0; i < num_sel; i++){
                console.log("Comparing " + + " to " + selected.linked_equipment[i]);
                // if a match is found
                if ( == 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";

        //save current form with updated status fields
        //insert created ticket into database

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?


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

Thank you! This works perfectly!