How to call a python function while clicking a button event

i have created a button on my custom doctype, so how i can call a function on the click event.

Hi,

Use cur_frm.cscript.fieldname=function(doc, dt, dn){} to add trigger on fields.

In your case,
cur_frm.cscript.call_me = function(doc, dt, dn){
/* function body*/
}

Regards,
Saurabh
New Indictrans Technologies Pvt Ltd.

Actually i need to call a function from python file. Not from the .js file.
Is it possible to call the python function from a .js file?

i just tried to call from csript, but getting some error messages.,

.js file


.py file

form

error

1 Like

am getting this message box while clicking the custom button i have created.

You have to declare a whitelisted method in your py module

@frappe.whitelist()
def call_me():
  msgprint("call me")

Then call from js

frappe.ui.form.on("[doctype]", "call_me", function(frm) {
  frappe.call({
    method: "myapp.mymodule.call_me",
    callback: function(r) { };
  });
});
2 Likes

Hi rmehta,
Actually I have the same issue and I’am not sure on where I have to declare the whitelisted method. Is it in the hooks.py of my app or in the .py file of my doctype?

Thx in advance for your help!

Put whitelisted method under .py file of doctype.

Ok thank you!

I got the error … ! App poly_clinic is not installed
poly_clinic is myapp here.
Thank you …!

@frappe.whitelist()
def test_method(document_name,cur_document):
    msgprint(Hai I am Here)

my js code :
frappe.ui.form.on("Lab Test", "test_invoice",
    function(frm) {
        frappe.call({
            "method": "poly_clinic.laboratory.test_method",
            args: {
                document_name: "Sales Invoice",
                cur_document: frm.doc.test_invoice
            },
            callback: function (data) {
        console.log(data);                
            }
        })
    });

And I am getting the error App poly_clinic is not installed

@jamsheer
Check your method path.
It should be like “app_name/app_name/module/doctype/method”
e.g. method: “erpnext.crm.doctype.lead.lead.make_quotation”

Thank you…
@Sangram

@frappe.whitelist()
def test_method(document_name,cur_document):
    msgprint(Hai I am Here)

my js code :
frappe.ui.form.on("Lab Test", "test_invoice",
    function(frm) {
        frappe.call({
            "method": "poly_clinic.poly_clinic.laboratory.lab_test.test_method",
            args: {
                document_name: "Sales Invoice",
                cur_document: frm.doc.test_invoice
            },
            callback: function (data) {
        console.log(data);                
            }
        })
    });

But Still the error remains :
AppNotInstalledError: App poly_clinic is not installed.
I have installed the erpnext over my app – is that a problem… ?

What is the path to the .py file?

It should be something like poly_clinic/poly_clinic/doctype/laboratory/lab_test.py, in which case the method call would be:

"method": "poly_clinic.poly_clinic.doctype.laboratory.lab_test.test_method"

this what my path and code :

ike poly_clinic/poly_clinic/laboratory/doctype/lab_test.py, in which case the method call would be:

“method”: “poly_clinic.poly_clinic.laboratory.doctype.lab_test.test_method”

I don’t think you have the path correct. Can you take a screen shot?

Thank you, Now its Working…