Create Custom Method for Existing doctype (extend doctype Class)

Hello,

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’

Thanks

Do you know hooks and doc events?

If you want to run code when sales invoice is saved, then write following doc event.

Write method code in any custom python file in your app.

For more inför refer hooks.

  1. https://frappe.io/docs/user/en/guides/basics/hooks

  2. https://www.youtube.com/watch?v=OFQP7ca0y_A&index=8&list=PL3lFfCEoMxvzHtsZHFJ4T3n5yMM3nGJ1W&t=0s

1 Like

Yes I know about that, but i don’t want run code on some event. I want run it in my custom function or in print template.

I thInk it’s not possible to inherit document class in apps

What is your use case?

can you share your code and use case?

Hello,

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.

If you want to fetch value form another doctype in print format. You can use
{{ frappe.db.get_value("[doctype]", “[name]”, “filename”) }}

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