How to make computations on custom field "BOM Stock Calculated"?

Hi guys, hope that someone can help me on my problem.
I tried to make a custom field for item and create (Diff rate) for the sum or difference of Valuation rate and Last purchase rate. is it possible?

Here’s my script :
frappe.ui.form.on(“doctype_name”, “valuation_rate”, function(frm) {
frm.set_value(“diff_rate”, flt(frm.doc.field1) + flt(frm.doc.field2));
})

frappe.ui.form.on(“doctype_name”, “last_purchase_rate”, function(frm) {
frm.set_value(“diff_rate”, flt(frm.doc.valuation_rate) + flt(frm.doc.last_purchase_rate));
})