Code Monkey home page Code Monkey logo

Comments (5)

shridarpatil avatar shridarpatil commented on August 23, 2024 1

@ahassoun
On click of button you can call an api with below snippet
In you python code you can add the below snippet

doc = frappe.get_doc("<doctype name>", "<name>")
frappe.get_doc(
                "WhatsApp Notification",
                notification_name
            ).send_template_message(doc)

from frappe_whatsapp.

simran-baliyan avatar simran-baliyan commented on August 23, 2024

+1

from frappe_whatsapp.

ahassoun avatar ahassoun commented on August 23, 2024

@ahassoun On click of button you can call an api with below snippet In you python code you can add the below snippet

doc = frappe.get_doc("<doctype name>", "<name>")
frappe.get_doc(
                "WhatsApp Notification",
                notification_name
            ).send_template_message(doc)

Thanks, worked perfectly.

from frappe_whatsapp.

ahassoun avatar ahassoun commented on August 23, 2024

@ahassoun can you share me code to which doctype you implemented? Complete code from button creation to trigger.

You can not directly call a Python method from the client script.

Here's how you can properly trigger a server-side method to send a WhatsApp message from a custom button in the "Sales Invoice" DocType:

Correcting the Client-side Script

  1. Variable Scope: The variable name isn't defined in your function. You should use frm.doc.name to get the name of the current document.
  2. Asynchronous Server Call: Use frappe.call to make an asynchronous call to a server-side method. You cannot directly call server-side methods like send_scheduled_message from the client-side without this.

Here's a corrected version of the client-side 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: '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"));
    }
});

Required Server-side Method

You'll need to implement a server-side method that the client-side script can call. This method will retrieve the WhatsApp Notification document and then call its send_scheduled_message method with the appropriate arguments.

Here's an example of what the server-side method could look like:

# Example server-side method in your custom app
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)}

Make sure to replace 'your_app_path.your_module_name.your_method' in the client-side script with the actual path to this server-side method, such as 'your_app.doctype.sales_invoice.sales_invoice.send_whatsapp_invoice'.

This setup ensures that when the "Send Invoice" button is clicked, it will make an AJAX call to the specified server-side method, which then handles the process of sending the WhatsApp Notification. Remember to adjust the server-side method path and any DocType names or field names to fit your actual setup.

from frappe_whatsapp.

github-actions avatar github-actions commented on August 23, 2024

Stale issue message

from frappe_whatsapp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.