Calling server script method in client script

Iam trying to call a method in client script, which is present in server script.

While iam doing this, iam getting error like below…

Screenshot from 2024-02-10 11-42-23

please help me to do this…

I will provide my client script and server script if needed…

Thanks in advance

@Kiranmai,
because the download method isn’t in the sales_invoice.py.
If you define the download, please check the path to the client script.

Thank You!

@NCP Thanks for your reply…

this is my server script code (sales_invoice.py).

@frappe.whitelist()
def download(docname):
document = frappe.get_doc(‘Sales Invoice’,{“name”:docname})
html_content = frappe.get_print(‘Sales Invoice’, document.name, print_format=‘Sales Invoice Custom Print’)
pdf = frappe.utils.pdf.get_pdf(html_content)
desktop_path = os.path.expanduser(“~/Downloads”)
current_datetime = datetime.now().strftime(“%Y-%m-%d_%H:%M:%S”)
file_name = f"{name}_{current_datetime}.pdf"
file_path = os.path.join(desktop_path, file_name)
# Save the PDF file
with open(file_path, “wb”) as f:
f.write(pdf)
return file_path

This is my client script code (sales_invoice.js)…

frappe.ui.form.on(‘Sales Invoice’, {
refresh: function (frm) {
frm.add_custom_button((‘Download Print’), function () {
frappe.call({
method: ‘sales_invoice.download’,
args: {
docname: frm.doc.name
},
callback: function (r) {
if (r.message) {
frappe.msgprint(
(‘Download successfully.’));
}
},
});
});
},
});

the file paths are :::

for server script : /home/kiranmai/Downloads/Hilltop/frappe-bench/apps/erpnext/erpnext/accounts/doctype/sales_invoice/sales_invoice.py

for client script : /home/kiranmai/Downloads/Hilltop/frappe-bench/apps/erpnext/erpnext/accounts/doctype/sales_invoice/sales_invoice.js

frm.call

please check the reference so you can understand of define the frm.call and frappe.call.

Please check the set_missing_values method

How to call in the sales_invoice.py

Frappe Ajax Call reference documentation.

I hope this helps.

Thank You!

1 Like

@NCP Its Wroking… Thank you

1 Like

Good day,
I am also trying to call a server script using a button and client script. Here is my test client script.

It should call the script when the button “recalculate attendance” is clicked. But I got this error.

image

even though the server script is whitelisted

I checked this document already, Frappe Ajax Call

Thank you.

whitelist method doesn’t worked on server side script.
If you want to call the whitelist method in server script then you have to watch this video on how I call it.

check the example:

1 Like