Calculate the fields

How to get the sum of the two fields in the same form using custom script? Can you explain me the code on how to do it? I’m new in erpnext.

frappe.ui.form.on("doctype_name", "validate", function(frm) {
  frm.set_value("field3", frm.doc.field1 + frm.doc.field2);
})

The above code will set sum of field1 and field2 in field3 on saving of the form.

1 Like

Hi THANK YOU VERY MUCH ! it’s a big help. Still, can I auto-calculate it without saving the form?

Following code will calculate field3, on change of field1 / field2:

frappe.ui.form.on("doctype_name", "field1", function(frm) {
  frm.set_value("field3", flt(frm.doc.field1) + flt(frm.doc.field2));
})

frappe.ui.form.on("doctype_name", "field2", function(frm) {
  frm.set_value("field3", flt(frm.doc.field1) + flt(frm.doc.field2));
})
4 Likes

Wow … Thank you very much … however, there is a logical error, the field3 displays wrong output, like i input on the field1 β€œ1” and the field2 β€œ1”, it displays β€œ11” on the field3.

Its working now! Thank you so much sir!

1 Like

Fieldtype of of all the fields should be Int / Currency / Float. Seems like you have set it as Data.