+ Start a Discussion
YashhYashh 

Want help in trigger

Each and every product should not overlap with the start and end date of another product. If it happens, then the new product should not get saved, an error should come


Example Suppose if any client book that date and second client also taking the same date either taking date between the previous client start date and end date so i want an error if this happens that record has not saved.
Best Answer chosen by Yashh
CharuDuttCharuDutt
Hii Yashh
Try Below Trigger
Please Change The ObjectName According To Your Org Object

trigger PreventDuplicateContacts on ObjectName (before insert) {
    Set <String> strDate = new Set<String>(); 
    Set <String> endDate = new Set<String>();
    for (ObjectName con:trigger.new) {
        strDate.add(con.Start_date__c);
        endDate.add(con.End_Date__c);
        
    }
    List <ObjectName> Lst = [SELECT Start_Date__c,End_Date__C FROM ObjectName WHERE Start_Date__c IN :strDate AND End_Date__c IN :endDate];

    for (ObjectName con:trigger.new) {
        If (Lst.size() > 0) {
          con.Start_Date__c.adderror( 'Duplicate Start Date Found In Existing Records.' );
          con.End_Date__c.adderror( 'Duplicate End Date Found In Existing Records.' ); 
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Yashh
Try Below Trigger
Please Change The ObjectName According To Your Org Object

trigger PreventDuplicateContacts on ObjectName (before insert) {
    Set <String> strDate = new Set<String>(); 
    Set <String> endDate = new Set<String>();
    for (ObjectName con:trigger.new) {
        strDate.add(con.Start_date__c);
        endDate.add(con.End_Date__c);
        
    }
    List <ObjectName> Lst = [SELECT Start_Date__c,End_Date__C FROM ObjectName WHERE Start_Date__c IN :strDate AND End_Date__c IN :endDate];

    for (ObjectName con:trigger.new) {
        If (Lst.size() > 0) {
          con.Start_Date__c.adderror( 'Duplicate Start Date Found In Existing Records.' );
          con.End_Date__c.adderror( 'Duplicate End Date Found In Existing Records.' ); 
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Amelia BelliAmelia Belli
Really helpful thread, that's what I am looking for the same solution. I've found the same information on here https://fasterapk.com/showbox-apk/