Sum Table Custom field under main Document

Dear Expert,

i have create one custom fields under delivery note item table (total rolls) and i would like to sum it under Delivery Note main doc custom field (Sum Total rolls). i have try to write the script below and no luck to make it works.

frappe.ui.form.on(“Delivery Note Item”, “total_rolls”, function(frm, cdt, cdn){
var d = locals[cdt][cdn];
frappe.model.set_value(d.doctype, d.name, “total_rolls”, d.total_rolls * 1);

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

frm.set_value(‘sum_total_rolls’, total);

});

Any advices. thanks again for the great help.

Try this?

frappe.ui.form.on("Delivery Note Item", {
    total_rolls: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        var total = 0;
        frm.doc.items.forEach(function(d) { total += d.total_rolls; });
        frm.set_value('sum_total_rolls', total);
    }
});

Hi Cpurbaugh,

Thanks for your kind help.
it works. but the main doc total rolls will total vlaue become 200,200.0 if the table item got more than 1x item.
anyway to make it sum up the total rolls instead split by 200,200?

Thanks

@justMilk I’m not understanding what you are saying, can you rephrase your last post? Also can you provide a screenshot of your table?

Hi Cpurbaugh,

Thanks for your kind reply. your custom script work, but did not total up all the total rolls number.
Summary:
i) 1x custom field under delivery note item table (total rolls)
ii) 1x custom field under delivery note doc (total rolls)

Thanks again for your great help.

@justMilk, Try with following code -

frappe.ui.form.on("Delivery Note", "validate", function(frm,doc) {
     var total = 0
     $.each(frm.doc["items"] || [], function(i, d) {
         total += d.total_rolls
     });
     frm.set_value('sum_total_rolls', total);
});
1 Like

Hi Priya_S,

Thanks for your kind help.
Still not luck to work as require.
Have try your custom script. it still show 200,400.000
which did not add up as 600rolls.

Thanks.

@justMilk, It works for me. What is the field-type of custom fields total_role and sum_total_rolls?

Hi Priya_S,

For total_role is data field.
For sum_total_rolls is float field.
should i change sum_total_rolls to become data field?

Thanks again for your kind help.

1 Like

change the total_role field type to Int or Float

1 Like

Set both fields as float and try again…

Thanks, Priya

1 Like

Hi Priya_s / makarand_b,

it works!!!
i have been keep look around to fix this for a week. Finally it works.

once again, thanks for the help :slight_smile:

1 Like