[How To] Catch Workflow State Change - Event Trigger

Using before_workflow_action: () => {}
how will i validate false on some condition?

you can use frappe.get_last_doc(doctype) in before save to get last workflow state and trigger if sate changed in on_update:

    def before_save(self):
        # Store the previous workflow state before saving
        last_doc = frappe.get_last_doc(self.doctype,{"name":self.name})
        self._previous_workflow_state = last_doc.workflow_state

    def on_update(self):
        # Compare current workflow state with the previous state
        if self.workflow_state != self._previous_workflow_state:
            self.after_workflow_action(self.workflow_state)```
1 Like

Thank you for this @aliriocastro .

I am using this in my code, but I am trying to halt the progression to the next workflow_state. SO if I use this trigger ( before_workflow_action ) and and my code detects that it is not a valid state change, how do I abort the change to the next workflow_state ?
Thank you

Hello, @trainingacademy to simply abort the next workflow state based on your condition you can use frappe.validated=false
frappe.throw(“Your message”)