You need to sign in to do that
Don't have an account?
Ryan Meyer
Second set of eyes on a basic trigger where AddError is not working
Hi - I have a basic trigger that works as follows: user types in HKG in a field and the trigger searches for airport__c records matching HKG and attaches them to a lookup field. I'm trying to add an error in case the IATA code (HKG) is not found, but it doesn't seem to be working. Am I putting it in the wrong place? Do I just make a if lookup__c is null adderror? Thank you very much for any assistance!
trigger ABInboundAirportMapping on Airline_Booking__c (before insert,before update) { Set<String> airportNames = new Set<String>(); for(Airline_Booking__c AB: trigger.new){ AirportNames.add(AB.Inbound_Airport__c); } List<Airport__c> airports = [SELECT Id, Name FROM Airport__c WHERE Name IN : airportNames ]; for (Airline_Booking__c AB : Trigger.new) { for( Airport__c ar : airports ){ if(ar.Name == AB.Inbound_Airport__c){ AB.Inbound_Airport_Lookup__c = ar.Id; } else { AB.addError('Unable to find inbound airport with that IATA code.'); } } } }
Hi Ryan,
Try below code.
trigger ABInboundAirportMapping on Airline_Booking__c (before insert,before update)
{
Set<String> airportNames = new Set<String>();
for(Airline_Booking__c AB: trigger.new){
AirportNames.add(AB.Inbound_Airport__c);
}
List<Airport__c> airports = [SELECT Id, Name FROM Airport__c WHERE Name IN : airportNames ];
for (Airline_Booking__c AB : Trigger.new) {
for( Airport__c ar : airports ){
if(ar.Name.toLowercase() == AB.Inbound_Airport__c.toLowercase()){
AB.Inbound_Airport_Lookup__c = ar.Id;
}
}
if(AB.Inbound_Airport_Lookup__c == null){
AB.addError('Unable to find inbound airport with that IATA code.');
}
}
}