Prompt dialog on click workflow state

My requirement is:

When admin user click on Reject from the workflow state, then the system should immediately prompt a dialog to choose a rejection reason. After choosing reason state should be changed to reject.

I found only refresh event works here, and state change to Reject before showing the dialog.

Here is my current code:

refresh(frm) {
		if (frm.doc.workflow_state && cur_frm.doc.workflow_state == 'Reject Processing' && !frm.doc.rejection_reason){
			frappe.prompt([
				{'fieldname': 'rejection_reason', 'fieldtype': 'Link', 'label': 'Reason', 'options': 'Sales Order Reject Reason', 'reqd': 1}],
				(value) => {
                    frappe.db.set_value('Sales Order', frm.docname, 'rejection_reason', value.rejection_reason)
                    .then(r => {
                        let doc = r.message;
                        //console.log(doc);
                    })
				},
				'Reason for Rejection',
				'Submit'
			);
		}
		
	}

Can you guys give me some hints about the issue?

You’re catching the refresh event, which gets called after save. Try catching before_workflow_action.

Edit: here’s a proper explanation: