hi alina, why do you need to create a trigger if it can be done by using simple validation rule. salesforce also says dont go for customization until it can't be done with configuration.
on the other part trigger should be like: trigger visit on Appointment__c (before insert) { for (Appointment__c app : Trigger.new){ if(some condition){ app.error(' '); } } }
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.
Trigger.new[0].addError('Incorrect Data');
}
it is right?
hi alina,
why do you need to create a trigger if it can be done by using simple validation rule. salesforce also says dont go for customization until it can't be done with configuration.
on the other part trigger should be like:
trigger visit on Appointment__c (before insert) {
for (Appointment__c app : Trigger.new){
if(some condition){
app.error(' ');
}
}
}
Could you please try with this code ?
trigger checktime on Appointment__c (before insert, before update) {
Map<id,DateTime> doc_time = new Map<id,DateTime>();
Set<id> docids = new Set<Id>();
docids.clear();
for(Appointment__c app: trigger.new){
if(app.Doctor__c != null){
docids.add(app.Doctor__c);
}
}
List<Appointment__c> applist = [Select Id, Time_of_Appoinment__c,Doctor__c from Appointment__c where Doctor__c = :docids ];
doc_time.clear();
for(Appointment__c apptemp : applist ){
doc_time.put(apptemp.Doctor__c, apptemp.Time_of_Appoinment__c);
}
for(Appointment__c app: trigger.new){
for(Id idtemp :doc_time.keySet() ){
if(app.Doctor__c == idtemp && app.Time_of_Appoinment__c == doc_time.get(idtemp)){
app.addError('This doctor already has an appointment at this time.');
}
}
}
}
Regards,
Sahana
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:
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