Custom workflow in Sales Order

I found this Some customs scripts that I use

And made a custom script. It’s now working but I need to manually press on Update button in the form for it to update because it’s trigged on the “refresh”.

I guess I need to change the trigger but I’m not sure what to use

frappe.ui.form.on(“Sales Order”, “refresh”, function (frm) {

//Variables for delivery and billing status
var NS = frm.doc.delivery_status === “Not Delivered” ;
var PS = frm.doc.delivery_status === “Partly Delivered” ;
var FS = frm.doc.delivery_status === “Fully Delivered”;
var NB = frm.doc.billing_status === “Not Billed” ;
var PB = frm.doc.billing_status === “Partly Billed” ;
var FB = frm.doc.billing_status === “Fully Billed” ;

var CN = frm.doc.workflow_state === "Cancelled" ;

//Variables for naming workflows. Your workflow names may vary
var READY = “Ready to ship”;
var COMPLETED = “Completed”;
var TOBILL = “To Bill”;
var CANCELLED = “Cancelled”;

//following if function checks to see if the document is submitted
if (frm.doc.docstatus == 1) {
    if (FS && NB) {
    cur_frm.set_value("workflow_state", TOBILL);
    } else if (FS && FB) {
    cur_frm.set_value("workflow_state", COMPLETED); 
} else if (NS && NB && !CN) {
    cur_frm.set_value("workflow_state", READY);
    } 
}

});