thanks for your answer. But i need to catch workflow state change. How can I do that with hook?
I need to run ideally python (or js - but python is more secure) code when workflow state is changed. This happens when user click on wokflow button Approve. I need to check if user can approve this specific doc. (this is based on custom logic)
I think workflow action (approve, reject…) doesn’t trigger form event, but it should trigger def on_update(self) in python file because workflow itself apply hook on on_update as well.
so I solved it. As @magic-overflow mentioned, workflow action doesn’t trigger form event in JS. It trigger on_update event in python. But in my case is workflow state changed also after submit, and then on_update event is not triggered. But i found thread where was written that if some fields are changed after submit, there is:
def on_update_after_submit(self):
which is triggered in this case. So If your workflow state is changing also after submit (or any other field - not only workflow state) this event is triggered.
For recapitulation: workflow state will trigger python events:
def on_update(self): # if state is changed before submit
def on_update_after_submit(self): # if state is changed after submit
Great! The advantage of the solution highlighted by @aliriocastro is however that it can be used in a Custom Script (Client Side). Server side scripts are not a convenient solution for most users
The form variable (usually passed as frm to the event trigger) should have a property selected_workflow_action. That will tell you which workflow action triggered the change event.
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)```