I have built a trigger to update Opportunity Line Items discount based on quantity before insert/update. There are custom fields to define the quantity tiers and discount to be assigned.
The code is returning values on o.Quantity and o.Discount, but not for the discount and quantity tiers. I suspect this might have something to do with not being able to access them with dot notation in the way I have done. How should this be done?
trigger opportunityLineItemTrigger on OpportunityLineItem (Before Insert, before update) {
if(Trigger.isExecuting && Trigger.isInsert && Trigger.isBefore){
for (OpportunityLineItem oppItems: Trigger.New){
ProductLineItemClass.updateLineItemDiscount(Trigger.new);
}
}
if(Trigger.isExecuting && Trigger.isUpdate && Trigger.isBefore){
for (OpportunityLineItem oppItems: Trigger.New){
ProductLineItemClass.updateLineItemDiscount(Trigger.new);
}
}
}
public class ProductLineItemClass {
public static void updateLineItemDiscount(List<OpportunityLineItem> oppItems ){
for(OpportunityLineItem o: oppItems){
If ((o.Quantity >= o.PriceBookEntry.Tier__c) && (o.Quantity<= o.PriceBookEntry.Tier_1_Upper_Quantity_Limit__c)){
o.Discount = o.PriceBookEntry.Tier_1_Discount__c;
} else If ((o.Quantity >= o.PriceBookEntry.Tier_2_Lower_Quantity_Limit__c) && (o.Quantity <= o.PriceBookEntry.Tier_2_Upper_Quantity_Limit__c)){
o.Discount = o.PriceBookEntry.Tier_2_Discount__c;
} else If ((o.Quantity >= o.PriceBookEntry.Tier_3_Lower_Quantity_Limit__c) && (o.Quantity <= o.PriceBookEntry.Tier_3_Upper_Quantity_Limit__c)){
o.Discount = o.PriceBookEntry.Tier_3_Discount__c;
}
}
}
}