How to fetch total unpaid balance of customer to sale inovoice

Hello every one. I am new to frappe and erpnext. As we can see the total unpaid balance of customer when we open the customer profile in erpnext. I want to fetch the total unpaid balance in the form of New Sale Invoice mean when i create a New sale invoice for a customer i can see the previous unpaid balance. Can anyone please guide me how to do it.

Where do you want to fetch it in the python file, javascript file or you needed it on your print format

When creating new sale invoice for any customer i want to fetch the unpaid balance for that customer. Like in screenshot as “Amir” is selected i can see its previous unpaid balance in the red box location.
Thanks in advance

Create a custom app for this functionality

basically i needed it to access in message notification sent via sms, basically i can only access the filed which are in the doctype. Mean if i am creating notification for sale invoice only fields that i can use to send via message are the doc fileds of sale invoice. Can You tell me if i can access the field of other doc type here." Sms notification Working in this flow. When I submit the invoice it sends message to customer with the fileds data which are mentioned in the message as shown in screenshot.

This will Help.

Okay, Can You let me know after adding custom script, What to do next as i cannot see the outstanding balance being fetch? I am totally new to erpnext.

Okay, got it. It worked.

okay, can you please see the video. As in sale invoice when i select customer its previous balance is being fetched but when i click the Include Payment (POS) check the balance becomes zero and then if i change the customer then the customer balance is being fetched. So can any one let me know, at first when i check the Include Payment (POS) box the balance should not become zero?