2

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 have been told that trigger.old and trigger.new is a way of achieving this, but I am having trouble understanding it completely.

I will provide my trigger so far. Many thanks.

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.');
    }
  }
}

1 Answer 1

1

Using Trigger.oldMap I was able to get the values this same Opp previously had. I was then able to add the additional check a.Area_Of_Hire__c != old.Area_Of_Hire__c to see if it has changed to this new value.

for(Opportunity a:trigger.new){
  Opportunity old;
  if (Trigger.oldMap != null)
    old = Trigger.oldMap.get(a.Id);
  if(count == 3 && (old == null || a.Select_Area__c != old.Select_Area__c) && 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 && (old == null || a.Select_Area__c != old.Select_Area__c) && 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.');
  }
}

Adding in the old == null is only useful if you are running this trigger on insert as well as update. If it's only on update you can replace (old == null || a.Area_Of_Hire__c != old.Area_Of_Hire__c) with a.Area_Of_Hire__c != old.Area_Of_Hire__c

9
  • 1
    Thanks for this. I am using this for an insert also. When i try to insert I am getting the Error message: Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FiveAsideQuantityTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.FiveAsideQuantityTrigger: line 5, column 1: [] (System Code) Commented Dec 11, 2014 at 16:11
  • 1
    My line 5 is Opportunity old = Trigger.oldMap.get(a.Id); Many thanks for your help. Commented Dec 11, 2014 at 16:16
  • 1
    Thank you. The date field I wish to check if it has already been selected is called Date_Of_Hire__c. So where you have put a.Area_Of_Hire__c != old.Area_Of_Hire__c should I change to Date_Of_Hire__c? Commented Dec 11, 2014 at 16:32
  • 1
    There are no errors in the code but I am not getting the error message I need. I have created 4 different records all with 5-a-side pitch, 09:00 AM and the same date 11/12/2014. On the forth entry the error message should fire. The code you have provided has put me on the right track I think its just something small to change. Commented Dec 11, 2014 at 16:47
  • 1
    @AndrewHoban Could the issue be between Select_Area__c and Area_Of_Hire__c ? You seem to have both of those fields checking the same field. I'm not sure which one you need to be checking. I think that once you get this part working you are going to need to have your aggregate query take all of that into account at once and then use a map to grab the correct one you need.
    – dphil
    Commented Dec 11, 2014 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .