Overwriting javascript with custom one

Hi There,

On server, In leave_application.js file, there is existing custom script as below:

 frappe.ui.form.on("Leave Application", {
     ....//code removed
     employee: function(frm) {
                    frm.trigger("get_leave_balance");
            },
            leave_type: function(frm) {
                    frm.trigger("get_leave_balance");
            },
            from_date: function(frm) {
                    if (cint(frm.doc.half_day)==1) {
                            frm.set_value("to_date", frm.doc.from_date);
                    }
                    frm.trigger("calculate_total_days");
            },
          to_date: function(frm) {
                if (cint(frm.doc.half_day)==1 && cstr(frm.doc.from_date) && frm.doc.from_date != frm.doc.to_date) {
                        msgprint(__("To Date should be same as From Date for Half Day leave"));
                        frm.set_value("to_date", frm.doc.from_date);
                }

                frm.trigger("calculate_total_days");
         },
         get_leave_balance: function(frm) {
                    if(frm.doc.docstatus==0 && frm.doc.employee && frm.doc.leave_type && frm.doc.from_date) {
                            return frappe.call({
                                    method: "erpnext.hr.doctype.leave_application.leave_application.get_leave_balance_on",
                                    args: {
                                            employee: frm.doc.employee,
                                            date: frm.doc.from_date,
                                            leave_type: frm.doc.leave_type,
                                            consider_all_leaves_in_the_allocation_period: true
                                    },
                                    callback: function(r) {
                                            if (!r.exc && r.message) {
                                                    frm.set_value('leave_balance', r.message);
                                            }
                                    }
                            });
                    }
            },
    
    ...
    });

I have created a custom script on the client side. See below:

frappe.ui.form.on("Leave Application","get_leave_balance",function(frm) {
   alert("Custom");
});

On Leave Application page, when I click on Employee or Leave Type, the browser displays “Custom” but when I click on Fron Date or To Date field, the browser doesn’t display “Custom” instead it calculates leave balance and displays it. Why doesn’t to call the custom method same as when I click on Employee or Leave Type field? Thanks.

MP

I don’t see the function triggered from from_date and to_date

Sorry @rmehta. I forgot to past the function calculate_total_days that is being called by From_date and To_date. I have now pasted it below. As you can see this function calls (frm_trigger) get_leave_balance, so it should show “Custom” alert on the leave application page, shouldn’t it?

calculate_total_days: function(frm) {
                if(frm.doc.from_date && frm.doc.to_date) {
                        if (cint(frm.doc.half_day)==1) {
                                frm.set_value("total_leave_days", 0.5);
                        } else if (frm.doc.employee && frm.doc.leave_type){
                                // server call is done to include holidays in leave days calculations
                                return frappe.call({
                                        method: 'erpnext.hr.doctype.leave_application.leave_application.get_number_of_leave_days',
                                        args: {
                                                "employee": frm.doc.employee,
                                                "leave_type": frm.doc.leave_type,
                                                "from_date": frm.doc.from_date,
                                                "to_date": frm.doc.to_date,
                                                "half_day": frm.doc.half_day
                                        },
                                        callback: function(r) {
                                                if (r && r.message) {
                                                        frm.set_value('total_leave_days', r.message);
                                                        frm.trigger("get_leave_balance");
                                                }
                                        }
                                });
                        }
                }
        },

The reason I am having our own custom get_leave_balance function is because I can call another function within get_leave_balance which show “balance till today” in addition to “balance till end of year”. is my approach above correct? Is my understanding correct that if I re-define get_leave_balance as client side script then it overwrites the existing get_leave_balance? Thanks.
MP

@rmehta, any feedback?

Thanks
MP

@mayur_hotmail can you fix this using a server-side hook. I can’t think of way of stopping the standard call.

Added this: [enhancement] [minor] frappe.ui.form.off will clear standard events by rmehta · Pull Request #1554 · frappe/frappe · GitHub

You can add (after its merged)

frappe.ui.form.off("Leave Application", "get_leave_balance")

3 Likes

Thanks Rushabh.

Do you have an example or link for how to implement this using a server-side hook?

Hi @rmehta Can you please verify this

frappe.ui.form.off("Leave Application", "get_leave_balance")

“off” event not working

Has anybody managed to get frappe.ui.form.off functionality working?? :thinking:

It works in both Custom Scripts and in-app doctype_js doctype views js override.

frappe.ui.form.off("Leave Application", "get_leave_balance");
frappe.ui.form.on('Leave Application', {
    get_leave_balance: function (frm) {
        alert("hello world");
    }
});
1 Like

I am trying to get it working with the following code in a custom script:

frappe.ui.form.off("Request For Quotation", "get_suppliers_button");
frappe.ui.form.on('Request for Quotation', {
	get_suppliers_button: function (frm) {
        alert("hello world");
    }
});

But clicking the Get Suppliers button still triggers the function defined in the doctype js before trigging the custom code.

Any idea what I’m doing wrong?

One thing I noticed is that in the sample function get_leave_balance, it is called using frm.trigger:
frm.trigger("get_leave_balance");
While the get_suppliers_button in request_for_quotation.js is called using:
me.get_suppliers_button(me.frm);