Write a trigger that prevents the insertion of an Account if the Account's Annual Revenue is less than $50,000. The trigger should also prevent updates that would set the Annual Revenue to less than $50,000.
Hi Swati,
Try this:
trigger PreventLowAnnualRevenue on Account (before insert, before update) {
// Iterate through each Account record in the trigger context
for (Account acc : Trigger.new) {
// Check for insert operation
if (Trigger.isInsert) {
// Prevent insert if AnnualRevenue is less than $50,000
if (acc.AnnualRevenue != null && acc.AnnualRevenue < 50000) {
acc.addError('Annual Revenue cannot be less than $50,000.');
}
}
// Check for update operation
if (Trigger.isUpdate) {
// Prevent update if AnnualRevenue is being set below $50,000
Account oldAcc = Trigger.oldMap.get(acc.Id);
if (acc.AnnualRevenue != oldAcc.AnnualRevenue && acc.AnnualRevenue < 50000) {
acc.addError('Annual Revenue cannot be set to less than $50,000.');
}
}
}
}
Mark this helpful if it solves your query!