1

Hopefully will get some help here :

I've some validations written in trigger , which fires during update. An example is - Updated records shouldn't have intermingling 'dates'(start date and end dates) with each other and also with any already existing records

Ex : record 1 has start date and end date as  : 1st jan - 31st jan 
 record 2 :  2nd feb - 29th feb ( mutually exclusive as of now) 
then I can't update  dates in both of them 
with following dates for example : 
record 1 :  10 jan - 10th feb 
record 2  :   20 th jan -  20th march 

As start date of 2nd record is being updated with date, which happens to be in-between, start date and end date of first record. So error should occur

Now When trigger fires, it successfully check overlapping dates with 'ALREADY' existing records and Prevent update but if same lot(records which are being updated together, as shown in example above) contains such dates- then trigger fails to check or validates this and succesfully update records without any error, which is problem for me

Any suggestions, how I can put such validations. Trigger code is little complex so not posting here .

1
  • 1
    Is this a BeforeUpdate or an AfterUpdate trigger? Seeing more of your code would be very helpful, even if it's "pseudocode' that tells us what you do.
    – crmprogdev
    Apr 15, 2016 at 15:24

1 Answer 1

1

In your logic i am assuming that you are querying for records, applying logic, creating collections etc to validate against.

Your issue seems to be with records within the scope of the trigger that also fall into the validation area (queried records) or may be new records.

  1. Query for records that are not part of the trigger and get your validation criteria (Existing data not being modified):

    Object[] obj = [Select xxx From Object Where ID Not IN : trigger.new];
    
  2. Now loop through your trigger records (Existing data being modified or new records) and apply your logic adding the results to whatever collections hold the data to validate against

    for(Object o : trigger.new){
       //perform your logic
    }
    

Now you have evaluated records in the database and the records in the trigger and have the final results to validate against.

You must log in to answer this question.

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