Run on all event and all doctype

i want try running some method on all event and checking what event that running, just like webhook

i’m trying to use

doc_events = {
	"*": {
		"*": "path to my method"

}

it didn’t work

and i’m trying to override Document

override_doctype_class = {
    "frappe.model.document.Document" : "path to my Custom Document Class",
}

it didnt work too
tell me if anyone know the solution

If you want to override all the doctypes, just make a list of the (around 500 doctypes in ERPNext + Frappe + some apps) and add them to doc_events in hooks.py. But I would advise against overriding all the doctypes and all the events.

What is the actual problem you want to solve?

hi corentin, thank you for answering. i want to create custom doctype like webhook and it need to run on event depends in the list that created.

Okay, maybe you can automatically generate a Server Script for your “Custom Webhook” document. In you “Custom Webhook” doctype, on save, generate a Server Script for the specific combination of doctype + event you want to inspect.

You don’t have a lot of choice I think, everything happens in Document.run_method and there is not a lot of overrides possible there.

image

Upon further inspection, there is a way!

You can use the * selector for doctypes but not events:

doc_events = {
  "*": {
    "on_change": ...
  }
}

Yeah i use that and list all of event to hooks.py thats why i’m asking the question to know if there any better solution for that

If you know the list of (doctype, event) pairs at runtime, you can generate Server Scripts with a single line frappe.call("your.whitelisted.method", doc, "{{event}}"), but other than that I don’t know an answer to your original question.

You can always submit a PR with the following change and ask the team nicely to integrate it :smile:

 doc_events = frappe.get_doc_hooks()
-for handler in doc_events.get(self.doctype, {}).get(method, []) + doc_events.get("*", {}).get(method, []):
+for handler in doc_events.get(self.doctype, {}).get(method, []) + doc_events.get("*", {}).get(method, []) + doc_events.get("*", {}).get("*", []) + doc_events.get(self.doctype, {}).get("*", []):
 	hooks.append(frappe.get_attr(handler))

Reference: How to use doctypes from erpnext to custom app - #2 by NCP