1

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);
          }  
    }
 }
3

You need to write a trigger for Opportunity. The trigger you wrote is for OLI and thus will not fire when an Opportunity is updated

| improve this answer | |
  • I tried writing a trigger for Opportunity but I get the compile error: 'Loop must iterate over a collection type: SOBJECT:Opportunity' at the FOR Loop lines. Could you please post an edit example of my trigger so that it compiles on the Opportunity Object? – user8556 Oct 26 '14 at 22:15
1

You can use an IF( ) Function to accomplish this.

IF( Opportunity.Probability > 0.95,  PricebookEntry.Product2.CustomField1__c , "")

Please note, decimals require a 0 before the decimal to work Salesforce.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.