Try ERPNext Try Frappe Cloud Buy Support Partners Foundation

Calling a JS method on the Client Script

Hi, I want a POS invoice to not allow to finalize if the customer has an outstanding balance.
But if an exception should be made to the client, it offers him to save the invoice as a draft so that the administrator can submmit the invoice.

I looked in the POS Invoice code for the line of code that saves the document as a draft and clears the screen, but I can’t figure out how to call that method from the Client Script.
Please could you help me by telling me the correct way to invoke it?

This is my Client Script:

Client Script

# DocType: POS Invoice
# Apply to: Form
# Script:
frappe.ui.form.on('POS Invoice', {
    before_submit(frm) {
	if (frappe.session.user!='Administrator' && frm.doc.outstanding_amount > 0) {
	     // abort the operation
             frappe.validated = false;
	     // Show Dialog.
             frappe.warn('YOU HAVE AN OUTSTANDING BALANCE',
                         'Do you want me to save it for the Manager to validate it?',
                         () => {
                                // action to perform if Continue is selected
                                // method taked from: erpnext/erpnext/selling/page/point_of_sale/pos_controller.js
                                // method: save_draft_invoice
                                this.save_draft_invoice();     // <----------------- HERE IS MY PROBLEM
                               },
                         'Yes, save it', // Text of the Continue Button
                         false // Sets dialog as minimizable
                         );
          }
     }
});

The code you shared will automatically be invoked on submit(if it is enabled).

In your code, this is not defined. Check console log(CTRL+SHIFT+I) for error and debug.
Try replacing this.save_draft_invoice(); with a simple frappe.throw('Saved as draft'); or maybe frm.save_draft_invoice();