I have a doctype called Cash Book in which users make entries of daily expense.
I want that if someone tries to enter a entry of more than 10,000 then it will not allow user to save the document.
I guess it can be sorted via client script, can someone explain the solution ?
frappe.ui.form.on('Cash Book', {
validate: function(frm) {
// Directly access the numeric value of 'expense' field
let expenseValue = frm.doc.expense;
// Check if the entered expense is greater than 9,990 INR
if (expenseValue > 9990) {
frappe.msgprint(__('Expense cannot exceed 10,000 INR.'));
frappe.validated = false; // Prevent the document from being saved
}
}
});
One thing to think about: client scripts never provide security. With a small amount of knowledge, a user could bypass this restriction and submit an expense of any amount.
If you’re just looking to remind your accounts team of this rule, this script is probably fine. If you actually want to prevent the expense from being booked, however, you should use a server script.
frappe.ui.form.on('Cash Book', {
validate: function(frm) {
// Check if the entered expense is greater than 10,000
if (frm.doc.expense > 9,990.000) {
frappe.msgprint(__('Expense cannot exceed 10,000.'));
frappe.validated = false; // Prevent the document from being saved
}
}
});
and it does gives the pop up that the amount exceed, but the issue is it does not allow me to save any amount.
I tried with that script and it is working as expected.
frappe.ui.form.on('Cash Book', {
validate: function (frm) {
// Check if the entered expense is greater than 10,000
if (frm.doc.expense > 10000) {
frappe.msgprint(__('Expense cannot exceed 10,000.'));
frappe.validated = false; // Prevent the document from being saved
}
}
});