Try ERPNext Buy Support Partners Foundation

How to override method in frappe?

Hello,
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.
or
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
	customer_doc.save(ignore_permissions = True)
	frappe.db.commit()
return customer_doc.name

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:

12 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.

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:

7 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

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.

1 Like

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