Sample Trigger:
trigger AccTrigger2 on Account (before insert) {
//Condition I am adding and this should run before the other condition.
for(Account accObj: Trigger.new){
if(accObj.Name.equalsIgnoreCase('Test')){
accObj.addError('Name cannot be test');
return;
}
}
//Condition already present in the system
for(Account accObj: Trigger.new){
if(accObj.BillingState.equalsIgnoreCase('MA')){
accObj.addError('Please select different state');
}
}
}
Below records are coming into the trigger:
- Name = 'Test', BillingState = 'CA'
- Name = 'Test', BillingState = 'MA'
- Name = 'Acme', BillingState = 'MA'
- Name = 'Acme', BillingState = 'CA'
Output I am trying achieve:
Output 1(with return statement in the trigger):
Output 2(with return statement commented):
Question: Is there a way to update the trigger so that I can get the output I need. I cannot update the two conditions as one logic as the logic for each condition is very complex and we don't want to touch the existing logic.
==
instead ofequalsIgnoreCase
. They do the same thing, but the latter throws an exception if the field is null... – sfdcfox May 24 '17 at 0:13