Client Script HELP

Hi everyone,

What I want is for our users to make daily entries in the CashBook Doctype.
Now, I want to restrict them in a way that if they try to make an entry with a date from two or more days earlier, it will not be allowed. For example, if today is 05-01-25 and tomorrow is 06-01-25, then on 07-01-25, if they try to make an entry for the date 05-01-25, the system should prevent it and display a message stating that entries for dates older than 1 day are not allowed.

I tried this one but no luck.

frappe.ui.form.on('Cash Book', {
validate: function(frm) {
    // Get today's date
    let today = frappe.datetime.get_today();
    
    // Iterate through each row in the child table
    frm.doc.data_aez3i.forEach(row => {
        // Check if the date entered in the row is two or more days earlier than today
        let entry_date = frappe.datetime.str_to_date(row.date); // 'date' is the field name
        let date_diff = frappe.datetime.get_diff(today, entry_date); // Get the difference in days
        
        if (date_diff > 1) {
            frappe.throw(__('You cannot make entries for dates more than 1 day earlier than today. Row: ' + row.idx));
        }
    });
}
});

here are my field list. in which the date should be depend on expense date which is = date

Remove entry_date variable

frappe.ui.form.on("Cash Book", {
	validate: function (frm) {
		// Get today's date
		let today = frappe.datetime.get_today();

		// Iterate through each row in the child table
		frm.doc.data_aez3i.forEach((row) => {
			// Check if the date entered in the row is two or more days earlier than today 
			let date_diff = frappe.datetime.get_diff(today, row.date); // Get the difference in days

			if (date_diff > 1) {
				frappe.throw(
					__("You cannot make entries for dates more than 1 day earlier than today. Row: " + row.idx)
				);
			}
		});
	},
});

Or replace frappe.datetime.str_to_date with frappe.datetime.str_to_obj

It’s working thank you so much

1 Like