Sum column in child table and show total in parent field

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:

please look in this :

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

@dufani1 show me your code after editing, please.

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

});

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

});

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)

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

});
1 Like

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

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

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
}

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

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

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

@dufani1 PM Me to help you.

1 Like

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

@dufani1 I sent a private massage, please check.

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

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

4 Likes

I need urgent help:

I’m new to scripting and I’ve used the same code but it does not work and gave me below error:

Code used is:

frappe.ui.form.on(“Project Item”, {
contract_amount:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.scope.forEach(function(d) { total += d.contract_amount; });
frm.set_value(“total_contract_amount”, total);
refresh_field(“total_contract_amount”);
},
scope_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.scope.forEach(function(d) { total += d.contract_amount; });
frm.set_value(“total_contract_amount”, total);
refresh_field(“total_contract_amount”);
}
});

Fields are:

Parent total field is: total_contract_amount
child table name is: Project Item
child table name in parent doctype is: scope
column to be sum name in child table is: contract_amount