completed assignment
This commit is contained in:
31
force-app/main/default/triggers/DiscountOpportunity.trigger
Normal file
31
force-app/main/default/triggers/DiscountOpportunity.trigger
Normal file
@@ -0,0 +1,31 @@
|
||||
trigger DiscountOpportunity on Opportunity (before insert,before update) {
|
||||
Map<Decimal, Decimal> discountTiers = new Map<Decimal, Decimal>
|
||||
{
|
||||
100000 => 0.10,
|
||||
50000 => 0.05
|
||||
};
|
||||
|
||||
Decimal maxDiscount = 20000;
|
||||
|
||||
for (Opportunity opportunity : Trigger.New)
|
||||
{
|
||||
Decimal discountAmount = 0;
|
||||
|
||||
for (Decimal tierAmount : discountTiers.keySet())
|
||||
{
|
||||
if (opportunity.Amount >= tierAmount)
|
||||
{
|
||||
discountAmount = opportunity.Amount * discountTiers.get(tierAmount);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (discountAmount > maxDiscount) {
|
||||
opportunity.Discount_Amount__c.addError('Maximum discount exceeded!');
|
||||
} else {
|
||||
|
||||
opportunity.Discount_Amount__c = discountAmount;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user