ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

Custom Script not updating a custom field in auto-repeat workflow

I have a custom field “Outstanding Bills” that pulls the customer’s balance to the invoice. When I use the already generated invoice as the reference doc in the Auto-repeat workflow, the subsequent auto-generated invoice doesn’t update the correct outstanding amount of the customer. I am using the following custom script to get the outstanding customer balance. Kindly advice…

    frappe.ui.form.on('Sales Invoice', {
         refresh(frm) {
            cur_frm.cscript.custom_validate = function(doc) {
            return frappe.call({
            	method: "erpnext.accounts.utils.get_balance_on",
            	args: {date: frappe.datetime.nowdate(), party_type: 'Customer', party: doc.customer},
            	callback: function(r) {
            		doc.outstanding_balance = r.message;
            		refresh_field('outstanding_balance', 'accounts');
            	}
            });
    	};
    }});