How to calculate values dynamically?

Hello,

In a Child Table I have put three fields:

  1. Qty Manufactured
  2. Qty Rejected
  3. Qty Total

Here I want to Sum Qty Manufactured + Qty Rejected and then put the total in Qty Total which is a Read Only field.

How can we do this?

TIA

Yogi Yang

Here you go
Go to 1:56. Modify it to get what you want.

2 Likes

@YogiYang Can you please post:

  1. The name of the main doctype
  2. The name of the child doctype
  3. The fieldname of the child table
  4. The fieldname of each of the child table fields

Then I will give you the Client Script that you can use to achieve what you want.

2 Likes

Hello,

RouteCard

Operations

qty_mfged (for qty manufactured)
qty_reject (for qry rejected)
qty_actually_mfg (here we need to total qty_mfged + qty_reject)

TIA

Yogi Yang

@YogiYang You didn’t provide me with the fieldname of the child table in the main doctype, so I will assume that it is operations.

Create a Client Script for RouteCard doctype, apply to Form and use the following code.

frappe.ui.form.on('Operations', {
    operations_add: function(frm, cdt, cdn) {
        cdt.qty_actually_mfg = flt(cdt.qty_mfged) + flt(cdt.qty_reject);
        frm.refresh_field('operations');
    }
});

If it didn’t work for some reason, please let me know.

Hello @kid1194,

Thanks for the code.

You have written code using the word operations_add which I take is an event fired when a new row is added.

From where can we get list of all supported events for Child Table?

I am asking this because I will have to link the code flt(cdt.qty_mfged) + flt(cdt.qty_reject) to change event of qty_mfged and qty_reject as well as when the table gets loaded.

TIA

Yogi Yang

@YogiYang True, it is an event.

Based on the documentation, you can’t capture the change event of child table fields.

You can capture the change event of fields in main doctype RouteCard but I haven’t tried to capture the change of child table.

Also, you can make changes before save or on validate and loop over frm.doc.operations.

You can find everything about child table events here.
Child Table Events

2 Likes