How to "calculate" the field value in realtime?

I have 3 fields on my DocType: Field1 , Field2 and Field3

I need to set Field 3 = Field1 + Field2

But I need to do that, on UI, not on python code (server side) to show in real time to user, the result.

How can I do that?

Regards

3 Likes

Hello @fellipeh,

Please refer the below code.

total_sum = 0;
$.each(frm.doc.fee_amount || [], function(i, d) {
total_sum += flt(d.amount);
console.log(“::::frm:: total_sum:::”, total_sum);
});
refresh_field(“total_amount”);

Thanks.

2 Likes

Where I can add this code? Server script?

For example, I use this code for calculation

doc.rest_of_ammount = doc.contract_amount - doc.paid_amount

This is working very well but not real time. Do you have any suggestions?

1 Like

Hi @RustemHesenov,

Please apply server script and set DocType Event.

Then apply your code:

doc.rest_of_amount = doc.contract_amount - doc.paid_amount

Check below image:

Thank You!

@Solufy all are the same. But this isn’t working in real-time.

Hi @RustemHesenov,

is ProcurementContracts child doctype?

Hi @Solufy,

No, ProcurementContracts is the main doctype?

then will perfect work for you.

Please check your field name.

@Solufy first of all thank you for answering all my questions.

The code is working well. But calculation process is realized after the save record. The calculation is successful but not real-time. :no_mouth:

Use client scripting instead of server-side script if you want the results immediately.

Link to client script docs:

https://docs.erpnext.com/docs/v13/user/manual/en/customize-erpnext/client-scripts

1 Like

Hi @RustemHesenov,

For parent doctype:
Please apply and check it.

frappe.ui.form.on('Parent DocType',  {
    contract_amount: function(frm) {
        frm.set_value("rest_of_amount", frm.doc.contract_amount - frm.doc.paid_amount);
    }
});

frappe.ui.form.on('Parent DocType',  {
    paid_amount: function(frm) {
        frm.set_value("rest_of_amount", frm.doc.contract_amount - frm.doc.paid_amount);
    }
});

For child doctype:
please check it.


Child table column total set in parent field for check it.

Thank You!

1 Like