Total calculation from child table

i tried to calculate total of Work Completed from child table and make it into 100% or divided by its row…
i used below code but no effect.

cur_frm.cscript.calculate_total = function(doc,cdt,cdn){
var val = doc.goals || [];
var total =0;
for(var i = 0; i<val.length; i++){
total = flt(total)+flt(val[i].work_completed)/val.length;
}
doc.total_work_completed = flt(total);
refresh_field(‘total_work_completed’);

}

What version are you using?

@Jonathan_Lee, version 7

@OmarJaber, thanks your help but this code dose not work for me.

frappe.ui.form.on(“Appraisal Plan Goal”, “work_completed”, function(frm, cdt, cdn) {
var total = 0;
$.each(frm.doc.appraisal_plan_goal || [], function (i, d) {
total += flt(d.work_completed);
});
frm.set_value(“total_work_completed”, total);
});

Not sure about version 7, but when we were using version 10, the refresh_field does not work for child table until version 11.

How did we figure this out? Basically we used the console to output the value of the child table, from the console it reported the new value we set, but on the user’s end, it doesn’t gets updated/refreshed.

i used below code in custom script. i can sum up but i can’t divide by numbers of row after sum up. when divide i used: total = total + ((work[i].work_completed)/work[i])) this code not working

frappe.ui.form.on(“Appraisal Plan Goal”, “work_completed”, function(frm, cdt, cdn) {
var work = frm.doc.goals;
var total = 0
for(var i in work) {
total = total + work[i].work_completed
}

frm.set_value("total_work_completed",total)

});