Try ERPNext Buy Support Partners Foundation

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) {
	return frappe.call({
		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(doc.company));
			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?

1 Like

Anyone out there can help me with this issue? I still can’t populate the customer balance on Sales Invoice when I pull items from Delivery Note.

Add your function to refresh listener.

frappe.ui.form.on("Your doctype", {
  refresh: function(frm, cdt, cdn) {
    // your code here
  }
})
1 Like

I added the event trigger as you suggested, but it doesn’t seem to work. Below is the code I use

frappe.ui.form.on(“Sales Invoice”, {
refresh: function(doc) {
return frappe.call({
method: “erpnext.accounts.utils.get_balance_on”,
args: {date: doc.posting_date, party_type: ‘Customer’, party: doc.customer},
callback: function® {
doc.customer_balance = r.message;
refresh_field(‘customer_balance’, ‘accounts’);
}
});
}
});

I’m not sure where to put the function within the function. Sorry, I am very new to programming.

Can anyone help me with this particular case.

How do I write a validate custom script of the above function. I am at a loss as to where to put the “Validate” trigger for this function to get custom balance when I save the document.

frappe.ui.form.on("Sales Invoice", "validate", function(doc){
			frappe.call({
			method: "erpnext.accounts.utils.get_balance_on",
			args: {date: doc.posting_date, party_type: 'Customer', party: doc.customer},
			callback: function(r) {
			doc.customer_balance = r.message;
			refresh_field('customer_balance', 'accounts');
	}
    });
});

When I click save, it doesn’t do anything

I’ve figured out the way to validate this:

cur_frm.cscript.custom_validate = function(doc) {
return frappe.call({
	method: "erpnext.accounts.utils.get_balance_on",
	args: {date: doc.posting_date, party_type: 'Customer', party: doc.customer},
	callback: function(r) {
		doc.customer_balance = r.message;
		refresh_field('customer_balance', 'accounts');
	}
});

}

1 Like

Also, with the script described above, it will only fetch the customer balance on the date same as Sales Invoice posting date. If you want the current customer balance, you’ll need to set the arg: {date: frappe.datetime.nowdate() }

So the script would be like this:

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.customer_balance = r.message;
		refresh_field('customer_balance', 'accounts');
	}
});
2 Likes

Hellow,
How do you get this script to work in a Custom Print format?

You’ll have to create a custom field to show the customer balance and then use the custom field in your print format

could u make a small video for help

Hi, I tried this but dint work.Could you Kindly illustrate, I would like to have this script in a custom jinja print format.