ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

How to Override js function in erpnext v9


#1

Hello,

I am trying to override js function named “calculate_item_values” is available in (erpnext=>erpnext=>public=>js=>controllers=>taxes_and_totals.js) erpnext.
to do a calculation for an amount in sales order lines.

Below is my code, I added in my custom application by creating controllers directory but it not working.

frappe.provide(“erpnext.public”);
frappe.provide(“erpnext.controllers”);
console.log(":::::::");
erpnext.TaxesAndTotalsCustomization = erpnext.taxes_and_totals.extend({
setup: function(){},
calculate_item_values: function() {
var me = this;
console.log(":::::CUSTOM:::::me:::::",me);
},
})

It is more prior to me. Please help me on this.

Thanks in advance :slight_smile:


Pass data to new link doctype
#2

This won’t work because even after this, erpnext.taxes_and_totals is used to calculate the values and not your modified function. You can do this:

frappe.provide(“erpnext.public”);
frappe.provide(“erpnext.controllers”);

erpnext.TaxesAndTotalsCustomization = erpnext.taxes_and_totals.extend({
  setup: function(){},
  calculate_item_values: function() {
    var me = this;
    console.log(":::::CUSTOM:::::me:::::", me);
  },
});

erpnext.taxes_and_totals = erpnext.TaxesAndTotalsCustomization;

#3

Hello @netchampfaris,

Thank you for replying. :slight_smile:
I improved my code as per your suggestion but still it not working,
And also tried commands to remove cache.

bench migrate
bench build
bench clear-cache
and then reloaded the browser, but still not working. :thinking:
Thanks


#4

Make sure your js file is loaded. Maybe you’ll have to add it to build.json


#5

Where do I need to add build.json file?
because I am not aware of build.json file.

Yes, my js file is loaded because I added one console.log() at the beginning of my js file and that is print when I reload the browser, because of my js file path is added to hooks.py ( app_include_js = “”).

Thanks


#6

In that case, you dont need to add to build.json, You will have to check whether the erpnext.taxes_and_totals class is available before extending it. Try debugging it using the debugger statement in JS


#7

Hello @netchampfaris,

Sry for the late reply.
Thanks for the suggestion. I will try to fix it by the debugger.


#8

Hello @netchampfaris,

Finally, I got the solution to override js function through the prototype.
Below is my code and it works fine.

erpnext.taxes_and_totals.prototype.calculate_item_values = function(){
var me = this;
console.log(":::::CUSTOM:::::me:::::",me);
}

Thanks. :slight_smile:


#9

Hi,
Am trying to override the onload function of Item, from custom script. This is my code

frappe.provide(“erpnext.item”);

erpnext.item.prototype.onload = function()
{
frappe.msgprint(‘overriding onload from cust script’);
}

What am I missing?

TIA
Krithi


#10

Hello Krithi,

where did you put this code?
I mean create a New and separate application?
you have to put the code in public directory ( for ex : public=>js=> file.js ) and also add the path of that file in hooks.py. (app_include_js)

Thanks


#11

Yes. In a custom app,added a file under pubic/js/ and referred it in hooks under doctype_js.

Now tried adding under app_include_js also. Didn’t work. Still invokes core ‘onload’.