ERPNext Foundation ERPNext Cloud User Manual Blog Discuss Frappé* Donate

Overwriting javascript with custom one


#1

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


#2

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


#3

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


#4

@rmehta, any feedback?

Thanks
MP


#5

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

Added this: https://github.com/frappe/frappe/pull/1554

You can add (after its merged)

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


#6

Thanks Rushabh.

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


#7

Hi @rmehta Can you please verify this

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

“off” event not working