Validation for QTY Update in Sales Invoice


I am using the following code to update the total qty in the sales invoice.

frappe.ui.form.on(“Sales Invoice Item”, “qty”, function(frm, cdt, cdn) {
// code for calculate total and set on parent field.
total_qty = 0;
$.each(frm.doc.items || [], function(i, d) {
total_qty += flt(d.qty);
frm.set_value(“total_qty”, total_qty);


The problem I am facing is if I cancel and amend the invoice, the field total_qty is not updated as there is no validation for that ( It remains the same as the initial one)

Please help.