Hi,
i am trying catch workflow state change, because i need disable / not permit / specific workflow state change to user. It can’t be done through user role in my case.
I am trying catch Transition 1 - from Pending to Approved
I created workflow like this:
And I am trying catch workflow in JS:
validate: function (frm) {
console.log(frm.doc.workflow_state);
console.log("validate");
},
after_save: function (frm) {
console.log(frm.doc.workflow_state);
console.log("after save");
},
workflow_state: function (frm) {
console.log(frm.doc.workflow_state);
console.log("workflow_state_change");
}
and in python also:
def validate(self):
frappe.msgprint(frappe._("validate!"))
def on_update(self):
frappe.msgprint(frappe._("update!"))
But any of this event is not triggered. How can i catch this? Thank you very much.
ERPNext: v11.0.3-beta.17 () (develop)
Frappe Framework: v11.0.3-beta.19 () (develop)
rmehta
February 16, 2019, 5:37am
2
You need to use hooks to trigger events at the target DocType you want to control… https://frappe.io/docs/user/en/guides/basics/hooks (see section on Document Hooks)
Hi,
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)
Thanks.
@rmehta can you help? Or It is not clear?
THANKS
janecek.mato:
Hi,
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)
Thanks.
clarkej
February 19, 2019, 11:18pm
5
A search on ‘workflow_state’ ‘change’ and ‘transition’ turns up for eg these for ideas
I thought for the benefit of the community that I would share some of the custom scripts that I use.
//Lock Sales Order for Approver Review
frappe.ui.form.on("Sales Order", "onload", function (frm) {
if (user_roles.indexOf("Approver" == 0) && frm.doc.workflow_state == "Pending Approval") {
msgprint("Not Authorized");
frm.disable_save();
}
});
//Reloads the page if in review stage
frappe.ui.form.on("Sales Order", "validate", function (frm) {
if (frm.doc.workflow_state === "Pending Approval…
Hi
My use case is to capture the transition of workflow on doctype when it moves from state A to state B. I am using workflow_state_var as the default status parameter and I tried to capture the change and perform some actions (call whitelisted server functions) but unable to do so. Here’s the relevant part of code snippet:
frappe.ui.form.on('Lead Fetch', {
workflow_state_var: function(frm, doc) {
console.log("Test function")
},
So, basically I need to call a function when th…
Hi,
@clarkej thanks for your answer, in these threads is written that workflow state change will trigger validate event.
Here is gif, where in validate event is console.log, but event is not triggered:
https://imgur.com/aLXgmNp
Maybe I miss something. Am I doing something wrong?
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.
standard_queries = {
"User": "frappe.core.doctype.user.user.user_query"
}
doc_events = {
"*": {
"on_update": [
"frappe.desk.notifications.clear_doctype_notifications",
"frappe.core.doctype.activity_log.feed.update_feed",
"frappe.workflow.doctype.workflow_action.workflow_action.process_workflow_actions"
],
"after_rename": "frappe.desk.notifications.clear_doctype_notifications",
"on_cancel": [
"frappe.desk.notifications.clear_doctype_notifications",
"frappe.workflow.doctype.workflow_action.workflow_action.process_workflow_actions"
],
"on_trash": [
"frappe.desk.notifications.clear_doctype_notifications",
"frappe.workflow.doctype.workflow_action.workflow_action.process_workflow_actions"
],
1 Like
clarkej
February 20, 2019, 4:47am
8
To identify what is going on, set a breakpoint in the client side code for example here:
./sites/assets/js/form.min.js: function () { return this$1.script_manager.trigger(“before_save”); },
Follow this howto Chrome DevTools | Chrome for Developers
This notes events to check out
Can attach to standard doctype events using custom_<event_name> as shown in example above.
Have seen these events scattered in code… frappe and erpnext
refresh
onload_post_render
before_load
onload
validate
Can see these events triggered in form.js
before_save
before_submit
on_submit
after_save
before_cancel
after_cancel
To set a breakpoint on the server add this line import pdb; pbd.set_trace()
Here’s how 9. The Python Debugger
./apps/frappe/frappe/model/document.py: def run_before_save_methods(self):
./apps/frappe/frappe/model/document.py: - validate
, before_save
for Save .
./apps/frappe/frappe/model/document.py: self.load_doc_before_save()
./apps/frappe/frappe/model/document.py: self.run_method(“before_save”)
1 Like
Hi,
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
Thank you all for help!
5 Likes
is there any idea to capture it in js instead of python?
There is a Client Side Event called before_workflow_action
.
1 Like
Everyone may want to check this file:
}
frappe.workflow.get_transitions(this.frm.doc).then(transitions => {
this.frm.page.clear_actions_menu();
transitions.forEach(d => {
if (frappe.user_roles.includes(d.allowed) && has_approval_access(d)) {
added = true;
me.frm.page.add_action_item(__(d.action), function() {
// set the workflow_action for use in form scripts
me.frm.selected_workflow_action = d.action;
me.frm.script_manager.trigger('before_workflow_action').then(() => {
frappe.xcall('frappe.model.workflow.apply_workflow',
{doc: me.frm.doc, action: d.action})
.then((doc) => {
frappe.model.sync(doc);
me.frm.refresh();
me.frm.selected_workflow_action = null;
me.frm.script_manager.trigger("after_workflow_action");
});
});
});
There are two client-side events for this situation:
before_workflow_action: () => {}
after_workflow_action: () => {}
Regards,
Alirio.
16 Likes
wale
October 15, 2020, 6:40am
13
Thanks a lot for this @aliriocastro , it was very helpful
Cheers
3 Likes
adam26d
October 22, 2020, 8:16am
14
I used if statements
hooks.py
# Hook on document methods and events
doc_events = {
"Expense Entry": {
"on_update": "expense_request.api.make_journal_entry"
}
}
api.py
def make_journal_entry(expense_entry, method):
if expense_entry.status == "Approved":
# my code goes within the if
1 Like
wale
October 23, 2020, 8:02pm
15
adam26d:
I used if statements
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
Cheers
2 Likes
any way to catch which workflow button is being clicked from client side script?
peterg
July 18, 2021, 1:47pm
17
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.
1 Like
Thanks a lot mate. This trigger helps much