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
Andrew Hoban 6Andrew Hoban 6 

Trigger if a date from a date field has been selected three times

Hi all,

I am creating a trgger that will fire an error when a particular picklist value has been selected three times. This works and the error message displays.

I now need the error to display if a date from a  date field has been selected three times.

I am unsure how i would implement this inside my trigger.
Any help would be appreciated. Thanks.

Trigger:
trigger FiveAsideQuantityTrigger on Opportunity (before insert,before update) {

Integer count =[select count() from Opportunity WHERE Select_Area__c='5-a-side Pitch' AND Start_Time__c!=null];
for(Opportunity a:trigger.new){
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='09:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='10:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='11:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='12:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='13:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='14:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='15:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='16:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='17:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='18:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='19:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
if(count==3&&a.Area_Of_Hire__c=='5-a-side Pitch'&& a.Start_Time__c=='20:00'){
a.addError('5-a-side Pitch is fully booked at this time.');
}
}
}

 
SonamSonam (Salesforce Developers) 
In your tigger, you can use the trigger context variable - trigger.old and trigger.new to compare if date has been changed from its past value and can maitain a counter(number fiedl) which counts the number of times the value of date field has changed on the record and when the counter is at 3 and date is changed- you can throw an error :

read more:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
http://www.sfdc99.com/2014/02/25/comparing-old-and-new-values-in-a-trigger/
Andrew Hoban 14Andrew Hoban 14
Thankyou for your reply that has been very helpful. 

Would I need to use a map in the example i  have provided? I am strugging to fully undertant .new and .old. 

Thanks