I have a custom button Whatsapp- Send Invoice which send invoice pdf to customer.
I want to merger server and client script. can this function be achieved through client script only?
Client script
frappe.ui.form.on('Sales Invoice', {
refresh(frm) {
frm.add_custom_button(__('Send Invoice'), function() {
// Use frm.doc.name to get the current document's name
frappe.call({
method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.send_whatsapp_invoice",//'your_app_path.your_module_name.your_method', // Specify the correct path to your server-side method
args: {
docname: frm.doc.name,
notification_name: 'WN-0001' // Assuming 'WN-0001' is the name of your WhatsApp Notification DocType instance
},
callback: function(r) {
if (!r.exc) {
// Handle successful message sending here, e.g., show a message to the user
frappe.msgprint(__('WhatsApp message sent successfully.'));
}
}
});
}, __("Send WhatsApp Alert"));
}
});
Server Script
import frappe
from frappe.core.doctype.whatsapp_notification.whatsapp_notification import WhatsAppNotification
@frappe.whitelist()
def send_whatsapp_invoice(docname, notification_name):
try:
whatsapp_notification = frappe.get_doc('WhatsApp Notification', notification_name)
doc = frappe.get_doc('Sales Invoice', docname)
whatsapp_notification.send_scheduled_message(doc)
return {'status': 'success'}
except Exception as e:
frappe.log_error(frappe.get_traceback(), 'send_whatsapp_invoice Error')
return {'status': 'error', 'error': str(e)}