31 lines
840 B
Plaintext
31 lines
840 B
Plaintext
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;
|
|
}
|
|
}
|
|
|
|
} |