You need to sign in to do that
Don't have an account?
Dipthi
Plz help me to bulkify my code - add error to all records in the list
trigger AccountIndustryAnnualRevenue on Account (before insert , before update) {
List<Account> AccList = new List<Account>();
for(Account Acc : Trigger.new) {
If((Acc.Industry == 'Chemicals') && (Acc.AnnualRevenue <= 1200)){
AccList.add(Acc);
}
If(AccList.size() > 0) {
Acc.addError ('When Industry is ‘Chemical’, annual revenue cannot be less than 1200'); // logic is incorrect.
}
}
}
List<Account> AccList = new List<Account>();
for(Account Acc : Trigger.new) {
If((Acc.Industry == 'Chemicals') && (Acc.AnnualRevenue <= 1200)){
AccList.add(Acc);
}
If(AccList.size() > 0) {
Acc.addError ('When Industry is ‘Chemical’, annual revenue cannot be less than 1200'); // logic is incorrect.
}
}
}
for(Account Acc : Trigger.new) {
if(Acc.Industry == 'Chemicals' && Acc.AnnualRevenue <= 1200){
Acc.addError('When Industry is ‘Chemical’, annual revenue cannot be less than 1200');
//AccList.add(Acc); You do not need to use a list to add the errors.
}
}
This page has details on the behavior of addError while doing bulk updates.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_exceptions.htm
All Answers
for(Account Acc : Trigger.new) {
if(Acc.Industry == 'Chemicals' && Acc.AnnualRevenue <= 1200){
Acc.addError('When Industry is ‘Chemical’, annual revenue cannot be less than 1200');
//AccList.add(Acc); You do not need to use a list to add the errors.
}
}
This page has details on the behavior of addError while doing bulk updates.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_exceptions.htm
Thank you very much Ajay Mohanan :)