is there way how to create custom method for existing doctype in my custom app?
I want custom method for example for Sales Order doctype, but i don’t want change code of ErpNext app.
Yes, i know that i can hook existing document events. But i don’t want hook existing events, i want create new document method. I tried extend Document class:
from erpnext.selling.doctype.sales_order import SalesOrder
class SalesOrder(SalesOrder):
def custom_method(self):
return "TEST"
But it is not working i am still getting: ‘SalesOrder’ object has no attribute ‘custom_method’
main use case for me for now is get some custom data from doctype when printing document. For example I need to now if there is at least one Item with Item Group == “Services” in Sales order. Now I am doing it in jinja, but it is not nice solution:
{%set has_services = namespace(a=0)%}{% for item in doc.items %} {%if item.item_group == "Services" %}{% set has_services.a = 1%}{%endif%}{%endfor%}
And i want do it on server side. So in SalesOrder doctype there will be method like has_services(self) which will return 0 or 1.
Because i need show some part of document only if there is at least one Item with group “Services” in Sales Order.
This is only one of the examples, usecase can be unlimited, and I think that in some cases of the printing documents, it is not possible to do such things in jinja.
It is not about fetching value from another doctype, it’s about custom conditional logic. See my example, i need to know if there is at least one item with a specific item group in the Sales Order. This is not a fetching value from another doctype. It is a type of calculation (custom logic).
As far as I’m aware, you need to, in your custom app, override standard doctype classes and link it to where the custom implementation in your app (as you have already)
# hooks.py
# DocType Class
# ---------------
# Override standard doctype classes
override_doctype_class = {
"Sales Order": "app.module.path.CustomSalesOrder",
}
# your_custom_module_with_CustomSalesOrder
from erpnext.selling.doctype.sales_order import SalesOrder
class CustomSalesOrder(SalesOrder):
def custom_method(self):
return "TEST"