How can I override method of core doctype which is not class method or whitelisted method?

hello @moe01325
In Email Campaign , there is set_email_campaign_status method which runs daily as it is in schedular events. But this method has error as it is not updating status in database. And because of this issue Email Campaign is not working. So I want to override this method to solve this by saving updated status in database.

Earlier Email Campaign issue is discussed. But there was no solution.

So I have checked core methods and found error in this method which is responsible for this issue.

Call the path in hooks. And in that python import the core class.

In hooks,
ToDo:“customapp.overides.customToDo”

In python,
from erpnext.erpnext.module.doctype.doctype_py Import ToDo

class customToDo(ToDo):
def validate(self):
super().validate()
#your code

Here super is to run the core

This override Class method. I want to override core function which is also not whitelisted.

1 Like

Try Monkey Patch!

1 Like

Hello @Rajvi,

I’m facing the same problem, did you find any solution ?

If you really have to override, the practice is to do this :

  • override the whole class which contains the code of that function or method
  • inside the overrided code → put your custom code inside that function or method

by doing this the system will use your overrided class and your custom code inside the override file

@Rajvi Did you find any solution, I have same task

Recently i found a solution:

Suppose we have a function send_mail in email_campaign.py

path: apps/erpnext/erpnext/crm/doctype/email_campaign/email_campaign.py

there is a method send_mail which is not whitelisted and not under any class

Override steps:

  1. Create a .py file in your custom app and do changes in send_mail function
  2. Goto __ init __.py file add paths like this

Now it will override the send_mail function @Rajvi @Aghiles

1 Like

Can you please help me on same?

yes , you can do the same thing with other method which is not whitelisted or not under class.

yes , you can do the same thing with other method which is not whitelisted or not under class.

Thanks for sharing solution
But this monkey override will apply on the global
Erpnext for all sites not only the site where the custom app have the override
There is solution for it

can you please explain a bit more ??

you could check for the site within the custom app?

i mean this override apply on the global erpnext code for all sites
not only the site where override is made

How check site ?

from frappe.utils import cstr
site_name = cstr(frappe.local.site)

you can use this to check current site and based on this add your condition

@Vipul_Kumar Not worked, i tried still calling core method ,
method i want to override ( SCHEDULER method) :
bench execute: erpnext.stock.reorder_item.reorder_item

can you please share your code or any reference

@Vipul_Kumar
Yes sure

init file:

overided file:

i am performing method like this:bench execute erpnext.stock.reorder_item.reorder_item
erpnext hooks.py

error log i get: