Validate Duplication on two fields

Hi,
I want to validate form on the basis of two fields,
I’ve fields “employee_id” linked with Employee & another field is “period_start_date” data type is date.
I want to check if Employee & Period Start Date is already exist then it won’t allow to create another one with the same Employee & same Period Start Date.
Is there any possibilities to do this?

Yes, you can add custom script to validate these two fields against existing records.

if not frappe.get_value("DocType", filters={'employee_id': employee_id, 'period_start_date': period_start_date}):
   # do the stuff

@saurabh6790 many thanks for your reply,

I used this but its not allowing me to save & also not showing the popup msg.

frappe.ui.form.on("Time Tracking", "validate", function(frm) {

	if(frappe.get_value("Time Tracking", filters={'employee': employee, 'period_start_date': period_start_date}))
	{
		frappe.msgprint("Same Employee with the Same Date is already exist in record.");
		validated=false;
    		return false;
	}	
});

Can you please point out where is the mistake?
NOTE : Field name is not not “employee_id” its “employee”

Achieved by using this script.
But when i create form and after saving it i edit the form its validate me to edit anything because the record already exist with the same employee and same date.
seems like trigger issue, i just want this function to run while creating form.
can anyone point out that where am i wrong?

frappe.ui.form.on("Time Tracking", "validate", function(frm) {

frappe.call({
        method: "frappe.client.get_value",
        args: {
                doctype: "Time Tracking",
                fieldname: "name",
                filters: {
                        employee: frm.doc.employee,
			period_start_date: frm.doc.period_start_date,
			period_end_date: frm.doc.period_end_date
                }
        },
        callback: function(response) {
             var employee = response.message;
             if (employee) {
                  frappe.msgprint("Same Employee with the Same Time Period is Already Exist in Record.");
			validated=false;
    			return false;
		
             }
        }
});
});

Solved by using frm.doc.__islocal.

1 Like