Set the custom field as mandatory in before cancel in client script to add reason of cancellation?

frappe.ui.form.on(‘Sales Order’, {
refresh: function(frm) {
// Ensure the cancellation reason field is visible when the workflow state is ‘Request To Cancel’
frm.toggle_display(‘custom_cancellation_reason’, frm.doc.workflow_state === “Request To Cancel”);
},
before_cancel: function(frm) {
if (frm.doc.workflow_state === “Request To Cancel” && !frm.doc.custom_cancellation_reason) {
frappe.validated = false;
frappe.msgprint((‘Please provide a reason for cancellation’));
}
},
validate: function(frm) {
if (frm.doc.workflow_state === “Request To Cancel” && !frm.doc.custom_cancellation_reason) {
frappe.validated = false;
frappe.msgprint(
(‘Please provide a reason for cancellation before saving.’));
}
}
});

i want to add filed and make it mandatory when take action request to cancel based on workflow state and this client script didn’t work

It’s better way to handing to the server side:

 def validate(self):
    if self.workflow_state == 'Request To Cancel' and not self.custom_cancellation_reason:
        frappe.throw(_('Please provide a reason for cancellation before saving.'))

Or

If you want to use the script in the server script doctype, then try it.

if doc.workflow_state == 'Request To Cancel' and not doc.custom_cancellation_reason:
    frappe.throw(_('Please provide a reason for cancellation before saving.'))


this server script not working
want to add condition here to make filed reason for cancellation mandatory when take action request to cancel based on workflow state