ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

Custom script to get customer balance on Sales Invoice


I was looking through the forum with regards to custom script to get customer outstanding balance on to Sales Invoice and came across this post:

get customer balance
by using this code

cur_frm.cscript.customer = function(doc) {
		method: "erpnext.accounts.utils.get_balance_on",
		args: {date: doc.posting_date, party_type: 'Customer', party: doc.customer},
		callback: function(r) {
			doc.outstanding_balance = format_currency(r.message, erpnext.get_currency(;
			refresh_field('outstanding_balance', 'accounts');

I have successfully pull customer balance onto Sales Invoice everytime a customer is selected. However, if I am to click Get Items button and pull items from Sales Order, the customer outstanding balance will become zero, and I have to select the customer again in order for the customer balance to appear correctly.

I’m wondering if there’s any way to keep this from happening?