Sum column in child table and show total in parent field using client script

Good Day every one can anyone here know how to sum column in child table and show up the total in parent field using customization in client scrip the code of mine below thank you im newbie here in ERP

frappe.ui.form.on(“Stock Entry Detail”, {
srp1:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.items.forEach(function(d) { total += d.srp1; });
frm.set_value(“total_srp”, total);
refresh_field(“total_srp”);
},
items_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.items.forEach(function(d) { total += d.srp1; });
frm.set_value(“total_srp”, total);
refresh_field(“total_srp”);
}
});
!
SRP FIELD IN CHILD TABLE IS THE ONE I NEED TO COMPUTE
TOTAL SRP IN PARENT FIELD IS THE ONE I NEED TO PRODUCE OUTPUT

image|690x353

parent doctype name is: Stock Entry
parent total field is: total_srp
child table name is: Stock Entry Detal
child table name in parent doctype is: items
column to be sum name in child table is: srp1

this is the name of the table in parent field

this is the CHILD TABLE

thia is the field that I need the sum of all in SRP column

Hi @VSI_IT,

Please apply it.

eg.

frappe.ui.form.on('Stock Entry', {
    validate: function(frm,cdt,cdn) {
        set_total_srp(frm);
    }
});

function set_total_srp(frm) {
    var doc = locals[frm.doc.doctype][frm.doc.name];
    var total_srp = 0;
    $.each(doc.items, function(i, d) {
        total_srp += flt(d.srp1);
    });
    frm.set_value("total_srp",total_srp);
}

Reload and check it.

If not work then please set your variable your according.

Thank You!

1 Like

thank you I Appreciate it but still not working :frowning:

Hi @VSI_IT,

It’s working on my side in version 13.
When you click to save then will be total appears.

Again check it.

Thanks.