Validating doc fields via custom scripts gives error

Hi folks,
This is purely related to the validation of doc fields at client side.
Requirement: To validate additional_discount_percentage in “Sales Invoice” doctype such that user does not put negative discount percentage.

erpnext version:11

code snippet used:

frappe.ui.form.on("Sales Invoice", "validate", function(frm) {
        if (frm.doc.additional_discount_percentage < 0 {
            frappe.msgprint(__("You can not select negative discount"));
            frappe.validated = false;
        }
    });

Screenshot 1: Created a custom script to validate docfield.

Screenshot 2: Getting error log message whenever i try to create a sales invoice.

Please help me as what i had missed/omitted.
Thanks in advance!!!

try with msgprint(_()) instead of frappe.msgprint(_())

1 Like

@kumar404
you missed ‘)’ after condition
correct form
if (frm.doc.additional_discount_percentage < 0) {
frappe.msgprint(__(“You can not select negative discount”));
frappe.validated = false;
}

1 Like

My bad, Sometimes even a single missing parenthesis will cause a major issue.
You noticed it.

Thank you all for responding.

But a new issue is coming.
Let me share the screenshot after correction.

@kumar404
is this error due to above custom script?

After modification of the above code, as per changes suggested, I got the above error…

Here, the other code works fine i.e, console.log works fine.
Added new line of code cur_frm.add_fetch to work fine but no fruitful result yet.

/*
cur_frm.add_fetch(‘Sales Invoice’, ‘additional_discount_percentage’, ‘additional_discount_percentage’);

frappe.ui.form.on(‘Sales Invoice’, ‘validate’, function(frm) {
if (frm.doc.additional_discount_percentage < 0) {
msgprint(__(“You can not select negative discount”));
frappe.validated = false;
}
});
*/

console.log(“sales invoice validation”);

Any help please?

try this one,
frappe.ui.form.on(‘Sales Invoice’, {
validate: function(frm) {
if(frm.doc.additional_discount_percentage < 0) {
msgprint(‘You can not select negative discount’);
validated = false;
}
}
});

1 Like

No success.
I am trying with all trial and error concepts.

I had even used this below code at the beginning:
{% include ‘erpnext/selling/sales_common.js’ %};

Please help me out !!!

I am sharing the link which might be help for the complete custom script writers.
It has helped me.

Form Scripting in Frappe Framework by Basawaraj Savalagi Form Scripting in Frappe Framework | by Basawaraj Savalagi | Medium

2 Likes