I've written a trigger that updates some fields on the Opportunity Product object with Product values when the Opportunity reaches 95% probability.
The trigger works when I edit and save the OpportunityLineItem but does not update if I just change the Opportunity stage to 95%.
What adjustments do i need to make so that the fields are updated as soon as the Opportunity reaches 95%?
trigger OppProductNetValue on OpportunityLineItem (before insert, before update) {
Map<Id,Opportunity> parentopps = new Map<Id, Opportunity>();
Set<Id> pbeIds = new Set<Id>();
for(OpportunityLineItem oli : trigger.new) {
if(oli.PriceBookEntryId != null){
pbeIds.add(oli.PriceBookEntryId);
parentopps.put(oli.OpportunityId,null);
}
}
Map<id, PriceBookEntry> pbeMap = new Map<id, PriceBookEntry>(
[SELECT id, Product2.id, Product2.True_Price__c
FROM PriceBookEntry WHERE id in :pbeIds]);
parentopps.putall([select Id, Probability, Order__c from Opportunity where Id in :parentopps.keyset()]);
for(OpportunityLineItem oli : trigger.new) {
if((pbeMap.containsKey(oli.PriceBookEntryId))&& (parentopps.get(oli.OpportunityId).Order__c == 'Sales Line')&& (parentopps.get(oli.OpportunityId).Probability == 95)){
oli.Net_Value__c = pbeMap.get(oli.PriceBookEntryId).Product2.True_Price__c*oli.Quantity;
}else{
oli.Net_Value__c = (1-oli.Discount*.01)*(oli.Quantity)*(oli.UnitPrice);
}
}
}