Frappe Cloud Support Partners Foundation Frappe School

Button for customer credit balance

There should be a button to check Customer Credit Balance which opens the report in sales and i have to built it inside the doctype of name Order Book.

any idea, how to do it ?


Try this on order book js
frm.add_custom_button(__(‘Customer Credit Balance’), function() {
frappe.set_route(‘Form’,‘Report’,‘Customer Credit Balance’);


Hi ,
While solution given is perfect and you may implement it , However , I am keen to know what is the use case ? Do you want to restrict or allow booking of orders when the customer credit balance is not adequate ? If you want to allow the order booking to continue even when credit limit is breached , there is an option at customer master "
Bypass credit limit check at Sales Order" , you may safely use this , If you want to allow order booking even when credit limit is in sufficient but with an approval then you will need to configure erpnext accordingly, Please let me know your use case for further discussions


this opens the report and then i have to click on the show report inorder to get the balance, after that i have to choose the customer.
what i want is that it should automatically generate it based on the current customer(customer is a field in the doctype).

nothing of that sort. no such conditons applicable as now. just want the customer credit balance.

If you just want to see the outstanding balance of the customer while creating sales order/invoice, then create a custom field ‘outstanding balance’ and use the following custom script:

customer: function(frm) {{
method: “erpnext.accounts.utils.get_balance_on”,
args: {date: frm.doc.posting_date, party_type: ‘Customer’, party: frm.doc.customer},
callback: function® {
frm.doc.outstanding_balance = format_currency(r.message, erpnext.get_currency(;
refresh_field(‘outstanding_balance’, ‘accounts’);

THis will autopopulate outstanding balance for the selected customer.

Try this
frm.add_custom_button(__(‘Customer Credit Balance’), function() {
frappe.set_route(‘query-report’, ’ Customer Credit Balance’,

my client needs a button for it.
so that on clicking the button the customer credit balance report opens in sales for the customer which was selected in the field.

@Nakul_Sharma - thanks for sharing the script. This indeed useful.

If it’s not too much trouble, may you repost the script as I think it did not paste the right way.

If you check , you will see your script has this “®”

Here is the script
customer: function(frm) {{
       method: "erpnext.accounts.utils.get_balance_on",
       args: {date: frm.doc.posting_date, party_type: 'Customer', party: frm.doc.customer},
       callback: function(r) {
           frm.doc.outstanding_balance = format_currency(r.message, erpnext.get_currency(;
           refresh_field('outstanding_balance', 'accounts');
1 Like

@Nakul_Sharma- Thank you very much. This is greatly appreciated.:grinning:

Hi @Nakul_Sharma

This code only fetches the outstanding balance after the document has been saved.
My use case is as below.

  1. The Script fetches outstanding balance immediately after selecting the customer name (Before saving the document)
  2. If the outstanding balance is equal or excess to the total customer credit limit, the document doesnt get saved. It should show a dialogue box that Customer credit limit crossed hence the doc can not be saved.
  3. Now if user want to save this document - user has to submit this for approval to superior.

Can you help on this?