ERPNext Foundation ERPNext Cloud User Manual Blog Discuss Frappé* Donate

Calculate total score from child table


#1

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’);

});


#2

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.


#3

@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.


#4

Can you share me your code?


#5

@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)
}
});


#6

After you saved the form, total_work_plan not updated value?


#7

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


#8

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


#9

here is browser console


#10

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.


#11

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


#12

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)

#13

i run in browser console it give…
dd


#14

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


#15

its working in browser console.