Calculate total score from child table

I want to add all ‘Work Completed’ mark and divided by its row. please help for code.

i used below code:
frappe.ui.form.on(“Appraisal”, “plan_template”, function(frm, cdt, cdn){
var vil = doc.plan_template || [];
var total2 =0;
for(var i = 0; i<vil.length; i++){
total2 = (flt(total2)+flt(vil[i].work_completed)/vil.length);
}
doc.total_work_plan = flt(total2);
refresh_field(‘total_work_plan’);

});

I guess event plan_template never be called.

frappe.ui.form.on("Appraisal", {
     validate: function(frm){
         this.calculate_total_work_plan();
     },
     calculate_total_work_plan: function(){
         var total = 0;
         var row_length = cur_frm.doc.plan_template.length || 0;
         $.each(cur_frm.doc.plan_template || [], function(i, row) {
             total += row.work_completed/row_length;
         });
         cur_frm.set_value("total_work_plan", total)
     }
});

I created function calculate_total_work_plan which calculate total and assign to total_work_plan, this function will be called on event validate which happen before save, so you will see the total_work_plan amount after form saved.

If you want realtime update total_work_plan, you can call this function when work_completed value changed, cur_frm.trigger(“calculate_total_work_plan”). I haven’t tested it, you can try.

@magic-overflow, thanks for your code, i tried but no luck. this ‘Work_Completed’ data are from child table and its read only in parent table. Total_work_plan should change when Work_completed, but changes make from other page(other doctype), not from this page.

Can you share me your code?

@magic-overflow,
frappe.ui.form.on(“Appraisal”, {
validate: function(frm){
this.calculate_total_work_plan();
},
calculate_total_work_plan: function(){
var total2 = 0;
var row_length = cur_frm.doc.plan_template.length || 0;
$.each(cur_frm.doc.plan_template || [], function(i, row){
total2 += row.work_completed/row_length;
});
cur_frm.set_value(“total_work_plan”, total2)
}
});

After you saved the form, total_work_plan not updated value?

event can’t save also. when i click save button, page become colour flatted

oh. Can you check error in browser console? and post screenshot here

here is browser console

You have error cur_frm.open_gride_row, it should be cur_frm.open_grid_row();

You have to fix your code before add mine.

when i remove code that you give. i can save it, but when i put that code, it can’t

Can you run below code in browser console?

         var total = 0;
         var row_length = cur_frm.doc.plan_template.length || 0;
         $.each(cur_frm.doc.plan_template || [], function(i, row) {
             total += row.work_completed/row_length;
         });
         cur_frm.set_value("total_work_plan", total)

i run in browser console it give…
dd

Did the total_work_plan change value when run the code in browser console?

its working in browser console.