How to override method in frappe?

In custom_app module_name.py

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

def shoutout(self):
	print("Yay!")

def before_cancel(self):
	self.shoutout()
	self.update_time_sheet(None)

def build_my_thing():
	SalesInvoice.shoutout = shoutout
	SalesInvoice.before_cancel = before_cancel

:hammer: python console :hammer:

In [1]: from custom_app.module_name import build_my_thing

In [2]: build_my_thing()

In [3]: sales_invoice = frappe.get_doc("Sales Invoice", "SINV-00165")

In [4]: sales_invoice.shoutout()
Yay!

In [5]: sales_invoice.cancel()
Yay!

more:

17 Likes

@revant_one
Thanks for the solution. I have marked this as solution as its working fine for overriding class method.

However this will not work in my case, as I need to override method which doesn’t belong to any class.

https://github.com/frappe/erpnext/blob/develop/erpnext/accounts/doctype/sales_invoice/pos.py#L372

1 Like

@revant_one I have been looking for such a method for a while. This is great pointer.
Quick Question - How is this loaded in the ERPNext context? Would you call build_my_thing() function on load of the core doctype? I’m no able to grasp when the bench would load the overridden function…

call build_my_thing just before you need to do overridden action.
onload, before frappe.get_doc or frappe.new_doc.

also, just like classes same thing is possible with modules.
Try it in bench console

Thanks @revant_one I tried using befoe_insert and onload. It works, but works inconsistently. Sometimes the custom method is called, but then other times, original method is called…doctype needs to be reloaded to call the custom method again…I dont know when it happens/ any thoughts?
Thanks.

Continued here

This thread is really popular. So I am posting this here.

You can now override a standard DocType class via hooks:

https://github.com/frappe/frappe/pull/11527

13 Likes

Is this different to this existing hook?

override_whitelisted_methods

That only works for whitelisted methods, which are those methods designated accessible to javascript.

@netchampfaris what about the method not belong to a class?

Like this file frappe/frappe/utils/data.py

1 Like

Other form that you can use to override methods in an application is call build_my_thing function at the end of hooks.py file. Specially userful when the method is in a class controller. We are testing that solution but apparently it works.

2 Likes

how to override the python method (not a whitelisted method and also not a method inside the class) which is in the erpnext app @rmehta @netchampfaris @revant_one @Mohammed_Redha @peterg

Did you check below before tagging to so many people ? What you have done so far and what are you trying to do ?

@sanjay yes I tried, I can able to override the whitelisted erpnext method from custom app via hooks.py and also a method inside the class, but i could not able to override the python method

Were you able to achieve it? I’m using override_doctype_class of hooks but it’s not working…

Hi @netchampfaris I need to use override_doctype_class hook but not sure if I’m missing something because it’s not working for me. The method metioned in the override_doctype_class is not reached. Could you please help?

I think override_doctype_class hook is part of v13 and above.

1 Like

@revant_one @netchampfaris Is there any way to override the method not belong to a class?

2 Likes

You can refer below :

2 Likes