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

Sum column in child table and show total in parent field


#1

Please help I tried to run the following custom script but it is not work:

frappe.ui.form.on(“Project test invoice”, “validate”, function(frm) {
set_total_01(frm);
})
frappe.ui.form.on(“sum test table”, “qty_11”, function(frm, cdt, cdn) {
set_total_01(frm);
})
var set_total_01 = function(frm) {
var total_01 = 0.0;
$.each(frm.doc.table_2, function(i, row) {
total_01 += flt(row.qty_11);
})
frm.set.value(“total_01”, total_01);
frm.refresh();
}

parent doctype name is:Project test invoice
parent total field is: total_01
child table name is: sum test table
child table name in parent doctype is: table_2
column to be sum name in child table is: qty_11

Parent doctype:

Child table:

Result:


#2

please look in this :


#3

please @Mohammed_Redha I tried but still not work help me in my case, I am not a programmer


#4

@dufani1 show me your code after editing, please.


#5

I need just total of column, this is the code:

frappe.ui.form.on(“sum test table”, “qty_11”, function(frm, cdt, cdn){
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, “qty_11”);

var total = 0;
frm.doc.table_2.forEach(function(d) { total += d.qty_11; });

frm.set_value(‘total_01’, total_01);

});


#6

put this script under parent doctype:

frappe.ui.form.on(“sum test table”, “qty_11”, function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.table_2.forEach(function(d) { total += d.qty_11; });

frm.set_value(‘total_01’, total);
refresh_field("total_01");

});

#7

I got the following error:

SyntaxError: Invalid or unexpected token
at Class.setup (http://localhost:8080/assets/js/form.min.js?ver=1515226095.0:2641:18)
at _f.Frm.setup (http://localhost:8080/assets/js/form.min.js?ver=1515226095.0:172:22)
at _f.Frm.refresh (http://localhost:8080/assets/js/form.min.js?ver=1515226095.0:446:9)
at Class.load (http://localhost:8080/assets/js/form.min.js?ver=1515226095.0:87:33)
at http://localhost:8080/assets/js/form.min.js?ver=1515226095.0:73:10
at Object.callback (http://localhost:8080/assets/js/desk.min.js?ver=1515226095.0:5498:6)
at Object.callback [as success_callback] (http://localhost:8080/assets/js/desk.min.js?ver=1515226095.0:1437:16)
at _ (http://localhost:8080/assets/js/desk.min.js?ver=1515226095.0:1461:34)
at Object. (http://localhost:8080/assets/js/desk.min.js?ver=1515226095.0:1562:5)
at i (http://localhost:8080/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)


#8

Try again :

frappe.ui.form.on("sum test table", "qty_11", function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.table_2.forEach(function(d) { total += d.qty_11; });

frm.set_value("total_01", total);
frm.refresh_field("total_01");

});

#9

Dear @Mohammed_Redha it works but there is an issue that when I remove a raw the total not updated.


#10

For your reference :

https://frappe.io/docs/user/en/guides/app-development/trigger-event-on-deletion-of-grid-row

put the same code on delete


#11

Sorry @Mohammed_Redha for disturbance.
Shall I use this? Script under parent or child table ?
frappe.ui.form.on(‘Item Color’, {
color_remove: function(frm) {
// Deleting is triggered here
}


#12

@dufani1 write this code in parent js doctype:

frappe.ui.form.on("Project test invoice", {
table2_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.table_2.forEach(function(d) { total += d.qty_11; });

frm.set_value("total_01", total);
refresh_field("total_01");
	}
});

#13

@Mohammed_Redha

frappe.ui.form.on(“Project test invoice”, {
table_2_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.table_2.forEach(function(d) { total += d.qty_11; });

frm.set_value(“total_01”, total);
refresh_field(“total_01”);
}
});

sorry not work this is the result:


#14

Dear @Mohammed_Redha please advise regarding this issue if you have time.


#15

@dufani1 PM Me to help you.


#16

@Mohammed_Redha I did not got your point? الرجاء التوضيح


#17

@dufani1 I sent a private massage, please check.


#18

@dufani1 try this :

frappe.ui.form.on("sum test table", {
   qty_11:function(frm, cdt, cdn){
   var d = locals[cdt][cdn];
   var total = 0;
   frm.doc.table_2.forEach(function(d) { total += d.qty_11; });
   frm.set_value("total_01", total);
   refresh_field("total_01");
 },
   table2_remove:function(frm, cdt, cdn){
   var d = locals[cdt][cdn];
   var total = 0;
   frm.doc.table_2.forEach(function(d) { total += d.qty_11; });
   frm.set_value("total_01", total);
   refresh_field("total_01");
   	}
   });

#19

Thanks a lot @Mohammed_Redha the following code working properly:

frappe.ui.form.on(“sum test table”, {
qty_11:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.table_2.forEach(function(d) { total += d.qty_11; });
frm.set_value(“total_01”, total);
refresh_field(“total_01”);
},
table_2_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.table_2.forEach(function(d) { total += d.qty_11; });
frm.set_value(“total_01”, total);
refresh_field(“total_01”);
}
});