Overlap validation

i’m using below code for avoiding overlapping for a custom field called facilitator in Event document so I need to check facilitator name against date so if he has another document saved in the same event start and end time not able to save the document but the code not working properly

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

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

Any Update

You need to write a python function for this or handle it in the custom script. I understand what you’re trying to do but this code doesn’t make much sense.

@Alaa_Badri

Try this.

frappe.ui.form.on("Event", "validate", function(frm) {
if(frm.doc.__islocal){
frappe.call({
        method: "frappe.client.get_value",
        args: {
                doctype: "Event",
                fieldname: "name",
                filters: {
                         facilitator: frm.doc.facilitator,
			                   starts_on: frm.doc.starts_on,
			                   ends_on: frm.doc.ends_on
                }
        },
        callback: function(response) {
             var facilitator = response.message;
             if (facilitator) {
                      frappe.msgprint("Same facilitator with the Same Time Period is Already Exist in Record.");
			                validated=false;
    			            return false;
		
             }
        }
});
}
});

@shahid

Thanks for your response, unfortunately, the code is not working it only checks the validation in the first time and then change the date to be in the same period it’s not validated for example if I have event from 11 July to 13 July and I make new event from 12 to 13 its save normally and didn’t make any validation

thanks for you contribution

Dear @tmatteson
thanks for your reply , did you have any idea for the code

Can you try below code:

frappe.ui.form.on("Event", "validate", function(frm) {
if(frm.doc.__islocal){
frappe.call({
        method: "frappe.client.get_value",
        args: {
                doctype: "Event",
                fieldname: "name",
                filters: {
						['facilitator', '=', frm.doc.facilitator],
						['ends_on', '>=', frm.doc.starts_on],
						['starts_on', '<=', frm.doc.ends_on]
                }
        },
        callback: function(response) {
             var facilitator = response.message;
             if (facilitator) {
                      frappe.msgprint("Same facilitator with the Same Time Period is Already Exist in Record.");
			                validated=false;
    			            return false;
		
             }
        }
});
}
});

Dear @Mukesh_Variyani
thanks for your response but there is a syntex error

SyntaxError: Unexpected token ,
    at Class.setup (http://erp.shiftits.com:81/assets/js/form.min.js?ver=1532943067.0:2691:18)
    at _f.Frm.setup (http://erp.shiftits.com:81/assets/js/form.min.js?ver=1532943067.0:172:22)
    at _f.Frm.refresh (http://erp.shiftits.com:81/assets/js/form.min.js?ver=1532943067.0:446:9)
    at Class.load (http://erp.shiftits.com:81/assets/js/form.min.js?ver=1532943067.0:87:33)
    at http://erp.shiftits.com:81/assets/js/form.min.js?ver=1532943067.0:82:7
    at Object.callback (http://erp.shiftits.com:81/assets/js/desk.min.js?ver=1532943067.0:5516:6)
    at Object.callback [as success_callback] (http://erp.shiftits.com:81/assets/js/desk.min.js?ver=1532943067.0:1437:16)
    at _ (http://erp.shiftits.com:81/assets/js/desk.min.js?ver=1532943067.0:1461:34)
    at Object. (http://erp.shiftits.com:81/assets/js/desk.min.js?ver=1532943067.0:1562:5)
    at i (http://erp.shiftits.com:81/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

Lets Try this

frappe.ui.form.on("Event", "validate", function(frm) {
if(frm.doc.__islocal){
frappe.call({
        method: "frappe.client.get_list",
		args: {
			doctype: "Event",
			filters: [
				["facilitator", "=", frm.doc.facilitator],
				["ends_on", ">=", frm.doc.starts_on],
				["starts_on", "<=", frm.doc.ends_on]
			],
			fields: ["name"]
		},		
        callback: function(r) {
             if (r.message) {
                      frappe.msgprint("Same facilitator with the Same Time Period is Already Exist in Record.");
			                validated=false;
    			            return false;
		
             }
        }
});
}
});

I have closed this parallel thread of yours @Alaa_Badri to avoid confusion wasted effort etc Validation error - query args escape_unicode error

dear @clarkej
it’s not the same here we trying custom script but another error is a server-side code please reopen it

Dear @Mukesh_Variyani

sorry it doesn’t make any validation

@Mukesh_Variyani @Alaa_Badri it dont work, because the ajax is running asyncronously, set the ajax as syncronous

frappe.ui.form.on("Event", "validate", function(frm) {
if(frm.doc.__islocal){
frappe.call({
        method: "frappe.client.get_list",
        async: false,
		args: {
			doctype: "Event",
			filters: [
				["facilitator", "=", frm.doc.facilitator],
				["ends_on", ">=", frm.doc.starts_on],
				["starts_on", "<=", frm.doc.ends_on]
			],
			fields: ["name"]
		},		
        callback: function(r) {
             if (r.message) {
                      frappe.msgprint("Same facilitator with the Same Time Period is Already Exist in Record.");
			                validated=false;
    			            return false;
		
             }
        }
});
}
});

@max_morais_dmm @Mukesh_Variyani
appreciated your support but also not working not have any response massage