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
sp13sp13 

trigger code conflict with lookup filter criteria

this is the trigger code:
trigger updateSeatTaken on Reservation__c (before insert, before update, after insert, after update) {
    List<Id> seatIdsForUpdate = new List<Id>();
    for (Reservation__c r : trigger.new) {
        seatIdsForUpdate.add(r.Seat__c);
    }
    List<Seat__c> seatsForUpdate = [SELECT taken__c FROM Seat__c WHERE Id IN: seatIdsForUpdate];

    for (Seat__c seat: seatsForUpdate) {
        seat.Taken__c = true;
    }
    update seatsForUpdate;
}


it works perfectly when the lookup field Seat__c has no filter.
but i need to have filter criterias for this lookup field.
these are the filter criterias:
Seat:Schedule__c     equals       Field        Reservation:Schedule__c  AND
Taken__c                     equals      Value       False

anyone knows what is the problem here?
Best Answer chosen by sp13
GlynAGlynA
The first thing I notice is "before insert, before update, after insert, after update".  It is rarely necessary to do the same thing both before and after and insert or update.  Because your trigger modifies an object other than the one for which it triggers, you should be doing this "after" only.  This might be the entire problem - System validation (including lookup filters) runs after the "before" trigger runs, but before the "after" trigger runs.  Because you're setting Taken__c to true in the "before" trigger, the lookup filters will find that the Reservation__c record is looking up to a seat that is taken.  If you only run this in "after", then the filter will pass before you change the seat to "taken".

-Glyn


All Answers

Rajesh SriramuluRajesh Sriramulu
Hi,

If u keep some conditions before assigning values then values are not populating right!!!

U just keep some debug logs so that u caan see where the exact problem is.

Regards,
Rajesh.
GlynAGlynA
The first thing I notice is "before insert, before update, after insert, after update".  It is rarely necessary to do the same thing both before and after and insert or update.  Because your trigger modifies an object other than the one for which it triggers, you should be doing this "after" only.  This might be the entire problem - System validation (including lookup filters) runs after the "before" trigger runs, but before the "after" trigger runs.  Because you're setting Taken__c to true in the "before" trigger, the lookup filters will find that the Reservation__c record is looking up to a seat that is taken.  If you only run this in "after", then the filter will pass before you change the seat to "taken".

-Glyn


This was selected as the best answer