function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlinaAlina 

What condition must be set so that the record to the doctor is not created if the record already exists at such a time?

trigger visit on Appointment__c (before insert) {
for (Appointment__c  app : Trigger.new){
if ()}
app.error('choose another time ');
      }
   }
}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Alina,

Greetings to you!

There are two ways to warn users about conflicts.
1. Using Process Builder and Flows
2. Triggers

Please refer to the below links which might help you further with the above requirement.

https://dayback.com/date-range-conflicts-salesforce/

If you want to use a trigger, first, Create a new (hidden) field: DBk_Conflict__c
Then, create a new validation rule: DBk_Conflict__c = "CONFLICT"
After that, you can write a trigger.

Below is the sample trigger:
trigger TimeOverlapping on Doctor__c (before insert, before update) {
    String thisId; //an event will always overlap with itself so omit it from the query
    Time startDate;
    Time endDate;
    
    for (Doctor__c newEvent : trigger.new){
        
        thisId = newEvent.Id;
        resource = newEvent.OwnerId;
        startDate = newEvent.Start_Working_Hours__c;
        endDate = newEvent.End_Working_Hours__c;
        
        List<Doctor__c> events = [SELECT Id FROM Doctor__c WHERE End_Working_Hours__c >= :startDate AND Start_Working_Hours__c <= :endDate AND ID != :thisId];
        
        if (!events.isEmpty()) { 
            newEvent.DBk_Conflict__c = 'CONFLICT';
        } else {
            newEvent.DBk_Conflict__c = '';
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
AlinaAlina
it is my firs day in trailhead and your code is very difficult to me(( maybe i can change something in my code i write above?
Avery BighamAvery Bigham
Using INSERT IGNORE effectively causes MySQL to ignore execution errors while attempting to perform INSERT statements. This means that an INSERT IGNORE statement which contains a duplicate value in a UNIQUE index or PRIMARY KEY https://www.upsers.one/