You need to sign in to do that
Don't have an account?

Triger- Before Inset and Before update ?
The code is with before insert, and I need tb before updating the code run. But when I use the Before update the code works incorrectly, it blocks everything.
trigger ReservDuplicateDate on Reserva__c (before insert) {
for(Reserva__c a:Trigger.new)
{
List<Reserva__c> lista=new List<Reserva__c>([Select ID,Data_de_terminio__c,Data_de_inicio__c,sala__c,Orador__c from Reserva__c]);
for(Reserva__c existentes:lista){
if(a.sala__c == existentes.sala__c && a.Data_de_inicio__c >= existentes.Data_de_inicio__c &&
a.Data_de_terminio__c <= existentes.Data_de_terminio__c) {
a.adderror('Reserva Ja existente na sala com a mesma data!');
}
if(a.sala__c == existentes.sala__c && a.Data_de_inicio__c <= existentes.Data_de_inicio__c &&
a.Data_de_terminio__c <= existentes.Data_de_terminio__c && a.Data_de_terminio__c >= existentes.Data_de_inicio__c ) {
a.adderror('Reserva Ja existente na sala com a mesma data!');
}
if(a.sala__c == existentes.sala__c && a.Data_de_inicio__c >= existentes.Data_de_inicio__c &&
a.Data_de_terminio__c >= existentes.Data_de_terminio__c && a.Data_de_inicio__c <= existentes.Data_de_terminio__c) {
a.adderror('Reserva Ja existente na sala com a mesma data!');
}
if(a.sala__c == existentes.sala__c && a.Data_de_inicio__c <= existentes.Data_de_inicio__c &&
a.Data_de_terminio__c >= existentes.Data_de_terminio__c ) {
a.adderror('Reserva Ja existente na sala com a mesma data!');
}
if(a.Orador__c == existentes.Orador__c && a.Data_de_inicio__c >= existentes.Data_de_inicio__c &&
a.Data_de_terminio__c <= existentes.Data_de_terminio__c) {
a.adderror('Esse Orador ja esta em cargo de uma reserva!');
}
if(a.Orador__c == existentes.Orador__c && a.Data_de_inicio__c <= existentes.Data_de_inicio__c &&
a.Data_de_terminio__c <= existentes.Data_de_terminio__c && a.Data_de_terminio__c >= existentes.Data_de_inicio__c ) {
a.adderror('Esse Orador ja esta em cargo de uma reserva!');
}
if(a.Orador__c == existentes.Orador__c && a.Data_de_inicio__c >= existentes.Data_de_inicio__c &&
a.Data_de_terminio__c >= existentes.Data_de_terminio__c && a.Data_de_inicio__c <= existentes.Data_de_terminio__c) {
a.adderror('Esse Orador ja esta em cargo de uma reserva!');
}
if(a.Orador__c == existentes.Orador__c && a.Data_de_inicio__c <= existentes.Data_de_inicio__c &&
a.Data_de_terminio__c >= existentes.Data_de_terminio__c ) {
a.adderror('Esse Orador ja esta em cargo de uma reserva!');
}
}
}
}
2) Added a limit to the query
3) In if condition, added a check to exclude the comparison with self record
All Answers
Try the following code it may be helpful for you:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
On a side note, you should look into few points : 1) Move your query outside of the for loop. I assume you do not need to query all the records for each record you receive in the trigger.
2) Put a limit in your query. You should not try to query all the records present in the DB.
I already tried this, the moment I use the before update the triger blocks any type of insertion
2) Added a limit to the query
3) In if condition, added a check to exclude the comparison with self record