How to Sum column in a child table upon fetch from another doctype

Good day, guys.
I want to sum a column in the child table once the child table is fetched from Source DocType to another child table in Target Doctype . below is what is my code but not working. pls i need assistance
frappe.ui.form.on(“Subcontractor Payment Item”,{
amount: function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.subcontractor_payment_item.forEach(function (d) { total += d.amount; });
cur_frm.set_value(“total_amount”, total);
refresh_field(“total_amount”);
},

subcontractor_payment_item_remove: function(frm, cdt, cdn){
    var d = locals[cdt][cdn];
    var total = 0;
    frm.doc.subcontractor_payment_item.forEach(function (d) { total += d.amount; });
    cur_frm.set_value("total_amount", total);
    refresh_field("total_amount");
}