Math Operation on fields

Thank you for your reply,

I’ve been change the value in payment but nothing happen.
May i know what i have to set type of the field on “balance_need_to_pay”?
is i have to set read only or float?

The type of balance_need_to_pay should be float. Deciding upon whether it has to be read only or not depends on your usecase.

I don’t think that problem with field type but as for me float type is more logic and you also can make it read only if you want.

please try this one
frappe.ui.form.on(“Employee Receipt”, “payment”, function(frm) {
msgprint(frm.doc.total_need_to_pay - frm.doc.payment);

frappe.model.set_value(“balance_need_to_pay”, (frm.doc.total_need_to_pay - frm.doc.payment));
frm.refresh_field("balance_need_to_pay");
msgprint(frm.doc.balance_need_to_pay);

frappe.model.refresh()

});

May i know why i need to change the fieldname of “payment”, because the result will be on “balance_need_to_pay”.
and I already change the your given code but still not working.

here my code:

frappe.ui.form.on("Employee Receipt", "payment", function(frm) {
      	frappe.model.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay - frm.doc.payment));
	frm.refresh_field("balance_need_to_pay");
       	frappe.msgprint(frm.doc.total_need_to_pay - frm.doc.payment);
	frappe.model.refresh()
});

Thanks @strixaluco for your reply
I’ve been change the type of field to float. but not showing the result after the math logic.

have you received message? have you reload the page after changing the script?

yes, because client scripts works after changing some value, so if you don’t change value in the field you don’t receive any results… that’s why I think we should add script to the payment field

Each time i edit the custom script, I reload the page but nothing happen. And i received no message from the console.

and have you changed value in payment field? if you didn’t receive message you script doesn’t work, it isn’t running.

Im sorry. I think i got a little confused with changing value in payment field.
What do you mean by that?

add any value to the payment field

Yes i already put some value on that payment field. But the code is not running the math operation.

here is my example:

how about message? have you received something?

I received nothing.
:joy::sob:

is my code wrong?

Could you please try to run this script, don’t forget to reload the page and change value in payment field

I’ve been change the script.
here is the result:

hmmmm, very strange

please try this one

I just try copy and paste the code from this link https://discuss.frappe.io/t/trigger-recalculation-after-change-in-customfield/7394/15?u=iqbalzahir

here is the code:

frappe.ui.form.on("Employee Receipt", "payment", function(frm) {
       cur_frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay * frm.doc.payment));
       frappe.msgprint(frm.doc.payment);
});
frappe.ui.form.on("Employee Receipt", "total_need_to_pay", function(frm) {
       cur_frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay * frm.doc.payment));
       frappe.msgprint(frm.doc.total_need_to_pay);
});
frappe.ui.form.on("Employee Receipt", "balance_need_to_pay", function(frm) {
       cur_frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay * frm.doc.payment));
       frappe.msgprint(frm.doc.balance_need_to_pay);
});

It worked when i edit the current employee receipt. but when i want to create the new employee receipt, it slow down the browser.
here is the pic:


it just stucked!

here is the error from console:

Error: Script terminated by timeout at:
Qa@http://localhost:8001/assets/frappe/js/lib/jquery/jquery.min.js:3:19975
hide@http://localhost:8001/assets/frappe/js/lib/jquery/jquery.min.js:3:22460
n.fn[b]@http://localhost:8001/assets/frappe/js/lib/jquery/jquery.min.js:3:29290
toggle@http://localhost:8001/assets/frappe/js/lib/jquery/jquery.min.js:3:22529
n.fn[b]@http://localhost:8001/assets/frappe/js/lib/jquery/jquery.min.js:3:29290
refresh_input@http://localhost:8001/assets/js/desk.min.js?ver=1504667907.0:4480:21
refresh@http://localhost:8001/assets/js/desk.min.js?ver=1504667907.0:4279:115
_f.Frm.prototype.watch_model_updates/<@http://localhost:8001/assets/js/form.min.js?ver=1504667907.0:300:33
runner/<@http://localhost:8001/assets/js/desk.min.js?ver=1504667907.0:7114:21
each@http://localhost:8001/assets/frappe/js/lib/jquery/jquery.min.js:2:2859
runner@http://localhost:8001/assets/js/desk.min.js?ver=1504667907.0:7112:4
trigger/<@http://localhost:8001/assets/js/desk.min.js?ver=1504667907.0:7131:12
  jquery.min.js:3:19975
TypeError: data.message.search is not a function[Learn More]  desk.min.js:925:6

Do you know how to fix it?

I suppose that problem because when you change value in “total_need_to_pay”, script is trying to execute but you don’t have value in payment field and then receive the error

Yeah I think so. Because the current receipt already have the value. So the code can run.