How to override method in frappe?

I am doing some customisation in POS?
I want to add vehicle number and customer type in POS.

I have successfully updated popup and javascript using javascript prototype and javascript class extend method. I have maintained this in another custom app using page_js hook.

Now, I am stuck at erpnext python code overriding?
In some cases we can use hooks before insert, after insert to write own python code, but this is not possible for current case as data is coming from local storage.

Is it possible to override exiting python methods?
How to access local storage in python?

Why not start contributing fixes instead :smile:

@rmehta The complete feature will be enable bold fields in pos.
For this current fix, we just need to update below code. and loop data (data is local storage customer list here)
Please advise on it.

def add_customer(data):
	customer_doc = frappe.new_doc('Customer')
	customer_doc.customer_name = data.get('full_name') or data.get('customer')
	customer_doc.customer_pos_id = data.get('customer_pos_id')
	customer_doc.customer_type = 'Company'
	customer_doc.vehicle_no = data.get('vehicle_no')
	customer_doc.customer_group = get_customer_group(data)
	customer_doc.territory = get_territory(data)
	customer_doc.flags.ignore_mandatory = True = True)

In custom_app

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

def shoutout(self):

def before_cancel(self):

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()

In [5]: sales_invoice.cancel()



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.

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?

Continued here

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

You can now override a standard DocType class via hooks:


Is this different to this existing hook?


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/

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


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 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?