Override_doctype_class and doc_events

Hello, I have my custom app and I am successfully using the hooks.py for both override_doctype_class and doc_events.

I am overriding functionality in Payment Entry doc/class.

The problem I am having

with override_doctype_class, is that this does not take effect when you Add a New Payment Entry. It only takes effect on an already saved/created payment entry when you load/open it from the list.

Preferably I do not need to override the class, I could simply override the validate function with my own validate function using doc_events…

BUT what doc_event triggers on a Add NEW Payment Entry before validate is executed?

If there is no answer to the above query for doc_events, then

is there a way to override the class and have this take effect on Add New Entry instead of only on loading and old one from the list?

Again I have been successful with both hooks.py methods working correctly… but they just do not fire when its a new one currently being created… The simplest fix would be to know the name of the doc_event that triggers before validate does!

Thank you!

Hi @esjastad were you able to override doc_events?

4 years old topic, do you need help with overriding doc events ?

Hi @janecek.mato, yes please.
I would like to override the doc_events hooks that are implemented in another app. How can I do this using my own app? Thank you.

doc_events = {
	"Sales Invoice": {
		"before_submit": "custom_app.utils.generate_einvoice"
	}
}

Its not so easy. When multiple apps define doc_events for the same document type and event (e.g., “Sales Invoice”: “before_submit”), all the hooks will be executed in the order the apps are loaded in the sites/apps.txt file for the specific Frappe site.

As i think only way is overriding whole class in hooks.py:

override_doctype_class = {
    "Sales Invoice": "custom_app_2.overrides.CustomSalesInvoice"
}

in your custom app:
custom_app_2/overrides.py:

from erpnext.accounts.doctype.sales_invoice.sales_invoice import SalesInvoice

class CustomSalesInvoice(SalesInvoice):
    def before_submit(self):
        # Full control on logic
        print("Only this logic runs")

But i have no tested this.

A good design is to extend the ERPNext standard functionality. Prone to less errors, makes use of the continuous updates, less coding, weak coupling. Not all logic currently implemented is wrong in the product.

Thank you for the replies. I am thinking of using monkey patch instead