Set Value of field to be equal of the sum of two fields

I have the following field names: A, B and C. I need to set the value of field C to be A + B in .js, how

I tried cur_frm.set_value(“C”, A + B) and I tried cur_frm.set_value(“C”, “A” + “B”) and it did not work.

Regards
Bilal

Please provide the code with real fieldnames. Will be easier for us to help you.

cur_frm.set_value(“boarder_total_paid_amounts”, board_salaries_and_peripherals + boarder_total_paid_amounts);

As in the below image, I need to set the value of field Total Paid Amounts to be equal for the sum of the two others fieds: Salaries & Its Peripherals + Cash & In Kind Benefits

SetValue_SumOfTwoFields

Note: Don’t worry about the symbols [100], [110], and [120] because they are only symbols for these fields.
Regards
Bilal

Correct statement would be:

cur_frm.set_value(“boarder_total_paid_amounts”, cur_frm.doc.board_salaries_and_peripherals + cur_frm.doc.boarder_total_paid_amounts);
1 Like

cur_frm.set_value(“boarder_total_paid_amounts”, cur_frm.doc.board_salaries_and_peripherals + cur_frm.doc.<cash_feldname>);

It did not do the arithmetic operation (the addition), but it puts the values of both fields next to each other.
For example:
board_salaries_and_peripherals is 50
board_cash_and_in_kind_benefits is 20

Now if I used:

cur_frm.set_value(“boarder_total_paid_amounts”, cur_frm.doc.board_salaries_and_peripherals + cur_frm.doc.board_cash_and_in_kind_benefits);

Then the value of boarder_total_paid_amounts is looking: 5020 and not 70

Appreciate the kindly help.
Regards
Bilal

Could be because the values are string and it should be converted to number (float and so on)?

Reagards
Bilal

yes if you have not taken field type as integer or float then you will have to typecast explicitly in your script
you can use

flt

1 Like

Try this:

   frappe.ui.form.on("[your-doctype-name]", {
        refresh: function(frm) {
        	// make calculation on the fields
    		var a = frm.doc.board_salaries_and_peripherals + frm.doc.board_cash_and_in_kind_benefits;
    		frm.set_value("boarder_total_paid_amounts", a);
    		frm.refresh_field("boarder_total_paid_amounts");
        }
    });
2 Likes

Thank You