Client Script to calculate Invoice Count

i need Client Script to calculate Invoice Count for the current sales invoice customer if it possible

@Osk

frappe.ui.form.on('Sales Invoice', {
    refresh: function(frm) {
        // Add a button to fetch and display the invoice count
        frm.add_custom_button(__('Show Invoice Count'), function() {
            frappe.call({
                method: 'frappe.client.get_value',
                args: {
                    doctype: 'Sales Invoice',
                    filters: {
                        'customer': frm.doc.customer
                    },
                    fieldname: 'count(name)'
                },
                callback: function(response) {
                    var count = response.message['count(name)'];
                    frappe.msgprint(__('Number of Invoices for this Customer: ') + count);
                }
            });
        });
    }
});

Try this.

thank you for the help

could you please give me more support to my issue

i dont want add new button to get total count

i want display total invoice count in custom field (invoice_count), this even must be work depend on customer in the current sales in voice m keep in mind that i add new tab break on the sales invoice include invoice_count field

@Osk Try this.

frappe.ui.form.on('Sales Invoice', {
    customer: function(frm) {
        if (frm.doc.customer) {
            // Fetch and update the total invoice count for the selected customer
            frappe.call({
                method: 'frappe.client.get_value',
                args: {
                    doctype: 'Sales Invoice',
                    filters: {
                        'customer': frm.doc.customer
                    },
                    fieldname: 'count(name)'
                },
                callback: function(response) {
                    var count = response.message['count(name)'];
                    frm.set_value('invoice_count', count);
                }
            });
        }
    }
});

there is no data in the invoice_count field when i enable Clint script

when i use first client script … bring me invoice count without any problem but i don’t want use button i need direct display count in the field

hii , try this one :

frappe.ui.form.on('Sales Invoice', {
    customer: function(frm) {
        // fetch the total count of invoices for the customer
        frappe.call({
            method: 'frappe.client.get_list',
            args: {
                doctype: 'Sales Invoice',
                fields: ['name'],
                filters: {
                    'customer': frm.doc.customer
                }
            },
            callback: function(r) {
                if (r.message) {
                    // Update the invoice_count field in the Sales Invoice form
                    frm.set_value('invoice_count', r.message.length);
                }
            }
        });
    }
});

how can i added a new event handler for the tab_break instead of button that mean
i want when choose tab break (custom_sales_survey) display invoice count in the

invoice_count field

did try this one?

i dont want use report

now this code is work and i got invoice count but i dont want use button event

i just want when i choose tab break (custom_tab_brake) bring to me data

frappe.ui.form.on('Sales Invoice', {
    refresh: function(frm) {
        // Add a button to fetch and display the invoice count
        frm.add_custom_button(__('Show Invoice Count'), function() {
            frappe.call({
                method: 'frappe.client.get_value',
                args: {
                    doctype: 'Sales Invoice',
                    filters: {
                        'customer': frm.doc.customer
                    },
                    fieldname: 'count(name)'
                },
                callback: function(response) {
                    var count = response.message['count(name)'];
                    frappe.msgprint(__('Number of Invoices for this Customer: ') + count);
                }
            });
        });
    }
});

@Osk Try this.

frappe.ui.form.on('Sales Invoice', {
    custom_tab_brake: function(frm) {
        // Fetch and display the invoice count when the custom_tab_brake tab is selected
        frappe.call({
            method: 'frappe.client.get_value',
            args: {
                doctype: 'Sales Invoice',
                filters: {
                    'customer': frm.doc.customer
                },
                fieldname: 'count(name)'
            },
            callback: function(response) {
                var count = response.message['count(name)'];
                frappe.msgprint(__('Number of Invoices for this Customer: ') + count);
            }
        });
    }
});

thank you for the help i solved the issue