Issue trying to call whitelisted PY function in Custom App

I have some Doctypes in a Custom App. One of them will work as a Production Order, and I need to call the functions stop_unstop and make_stock_entry from Production Order.
How can I do this?

In the case that I have to create my own functions and then call them from my Custom Doctype, how should I do it?

First I tried this JS function:

frappe.ui.form.on("Work Order", "refresh", function (frm)
{
    if (frm.doc.docstatus === 1)
        custom_app.stop_work_order(frm, status);
});

custom_app = {
    stop_work_order: function (frm, status)
    {
        frappe.call({
            method: "custom_app.orders.doctype.work_order.work_order.stop_unstop",
            args: {
                work_order: frm.doc.name,
                status: status
            },
            callback: function (r) {
                if (r.message) {
                    frm.set_value("status", r.message);
                    frm.reload_doc();
            }}
        });
    }
}

Then I tried this:

frappe.ui.form.on("Work Order", "refresh", function (frm)
{
    if (frm.doc.docstatus === 1)
        custom_app.stop_work_order(frm, status);
});

custom_app = {
    stop_work_order: function (frm, status)
    {
        frappe.call({
            type: "POST",
            args: {
                work_order: frm.doc.name,
                status: status
            },
            url: "/api/method/work_order/POST?run_method=stop_unstop",
            callback: function (r) {
                if (r.message) {
                    frm.set_value("status", r.message);
                    frm.reload_doc();
            }}
        });
    }
}

The PY code is this:

@frappe.whitelist()
def stop_unstop(work_order, status):
    w_order = frappe.get_doc("Work Order", work_order)
    w_order.update_status(status)
    w_order.update_planned_qty()
    frappe.msgprint(_("Work Order has been {0}").format(status))
    w_order.notify_update()

    return w_order.status

Always I received the message error 404 (NOT FOUND).

P.S.: I already visited the following links without success:

Check the dotted path of the whitelisted function that you write in the frappe call.
Also check indentation in the code. And make sure there’s an__init__.py file in the directory in which you are writing your .py file.
If the whitelisted function is written in doctype py file in the custom app, usually the dotted path is like below:

[custom_app].[custom_app].doctype.[doctype_name].[doctype_name].[whitelisted_function]

In you case it should this, I presume:

custom_app.custom_app.doctype.work_order.work_order.stop_unstop

You can open bench console and try to import your method.
e.g.
from custom_app.orders.doctype.work_order.work_order import stop_unstop

This will give you proper result whether the above path is working or not, or you can add the proper path according to your directory structure.
REF :

Thanks for you response @kartik.

I reviewed the identation and have written correctly the dotted path of the whitelisted function. Also I added some lines to the __init__.py file.

This is the content of __init__.py file:

from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document

@frappe.whitelist()
def stop_unstop(work_order, status):
        w_order = frappe.get_doc("Work Order", work_order)
        w_order.update_status(status)
        w_order.update_planned_qty()
        frappe.msgprint(_("Work Order has been {0}).format(status))
        w_order.notify_update()

        return w_order.status

The dotted path of the whitelisted function is the following:

custom_app.custom_app.doctype.work_order.work_order.stop_unstop

I have done all this and keeps returning the same error 404.

Thanks for you response @abhijit_kumbhar.

I have tried to import the method and it does not return anything, I assume that since there is no problem it does not return anything.

If you have written your function in__init__.py file and the location of the said file is

frappe-bench/apps/custom_app/custom_app/doctype/work_order/

then the path of whitelisted function would be

custom_app.custom_app.doctype.work_order.stop_unstop

I have rewritten the path to the function and I still receive the same error, I do not know why.

Did you run restart services for changes to apply?
Run sudo supervisorctl restart all

1 Like

Yes, and it is still returning the same error.

Any suggestions?

I need to know how to use whitelisted PY functions in my custom App.

share error

The error when I call the functions is Not found. The resource you are looking for is not available

image

Did you create file work_order.py manually ?

Yes, I created it manually, and the source code syntax of the PY file is correct.

You have to create Doctype Work Order through UI with custom checkbox unchecked.

Edit:
In order to uncheck the custom checkbox, you have to enable developer mode;
how-enable-developer-mode-in-frappe

Yes, I created it through the UI and deactivated the Custom checkbox

work_order.py should be created automatically, not manually.

Sorry, I misinterpreted your question.
I want to say that I modified the work_order.py file to create the functions that I need.

i think you should try this if your python logic is ok…

frappe.ui.form.on("Work Order", "refresh", function (frm)
{
    if (frm.doc.docstatus === 1){
        stop_work_order(frm, frm.doc.status);
    }
});


var stop_work_order: function (frm, status)
    {
        frappe.call({
            method: "custom_app.custom_app.doctype.work_order.work_order.stop_unstop",
            args: {
                work_order: frm.doc.name,
                status: status
            },
            callback: function (r) {
                if (r.message) {
                    frm.set_value("status", r.message);
                    frm.reload_doc();
            }}
        });
    }