Calculations in script

Hello,

I have created a parent doctype as “Costing Template”

In that there are different child tables as shown below:

Child table name for Sewing is “Costing Table3”
Child table name for Labels is “Costing Table4”
Child table name for Trims and Accessories is “Costing Table5”

So, I want to calculate the total of all the fields of Sewing and display it in “ttl1”, Labels total in “ttl2” and trim/accessories in “ttl3”




And finally calculate the total of “ttl1”, “ttl2”, “ttl3” and display its total result in “TOTAL 2”.

How can I do that?

I have tried with many script, but nothing worked.

Can anyone solve this?

Please help.

Thanks & Regards,
Sujay

Hi @Sujay,

you can add a script like this:

frappe.ui.form.on("Costing Template", {
  validate: function(frm) {
    // this function is called when the record is saved
    update_totals(frm);
  }
}); 

function update_totals(frm) {
    var total2 = 0;
    // calculate total of each line in sewing
    // assuming your child table name (not that of the doctype, that of the table) is sewings
    frm.doc.sewings.forEach(function (item) {
        var ttl1 = item.threads + item.elastic1 + item.elastic2;
        item.ttl1 = ttl1;
        total2 += ttl1;
    });
    // do the same for the other child tables

    // apply total 2
    cur_frm.set_value("total2", total2);
}

Hope this helps.

Hello @lasalesi ,

I have added in this manner.

But its not working.

Also, after entering the data in different child field, when I Click on save it is not getting saved.

The extension looks good, can you please check your browser console (press F12) and share error messages? There might be some error in the script… If it does not save this correlates with the fact that this insert is added in the validate block, an error will prevent saving…

Hello @lasalesi ,

Here is the screenshot.

Can you try and open the console and then click “Save”? There should be some sort of output…