In frappe.ui.Dialog how to calculate a field value on change event of another field

Hello,

I have designed a dialog using frappe.ui.Dialog. In this dialog I defined 3 fields.

  1. qty_mfged
  2. qty_reject
  3. qty_total

When a user enters value in either field qty_mfged or qty_mfged I want to calculate the value and place it in field qty_total. The formula I want to apply is qty_mfged - qty_reject.

How to do this?

TIA

Yogi Yang

2 Likes

you can use somthing like this:

    let d = new frappe.ui.Dialog({
    title: 'Please enter value!',
    fields: [{
            label: 'Field Label',
            fieldname: 'field_name',
            reqd: 1,
            fieldtype: 'Data',
            onchange: function(e) {
                if(this.value){
                    
                }
            }
        }
    ],
    primary_action_label: 'Submit',
    primary_action(values) {
        var total = values['field_name']

        frm.set_value("total", total).then(function () {
            frm.save_or_update();
        });
        cur_frm.refresh()

        d.hide();
    }
});
1 Like

Hello,

Thanks for the code sample.

But how do I read value of the other field qty_reject in the dialog?

TIA

Yogi Yang

Hello,

This Client Script code is working in my case.

let mfged_val = flt(this.value);
let reject_val = flt(finish_batch_dialog.get_value('qty_reject'));
if(mfged_val > reject_val){
    finish_batch_dialog.set_value("qty_total", flt(mfged_val - reject_val));
}else{
    finish_batch_dialog.set_value("qty_reject", 0);
    finish_batch_dialog.set_value("qty_total", flt(mfged_val));
}

TIA

Yogi Yang

2 Likes