0

I have a before trigger that does some validation and throws an error when any of the criteria is true, but I am seeing that it is only working as an after update which should never be. Why is that?

trigger OpportunityValidationonClosed_Won on Opportunity (before update) {

    List<OpportunityLineItem>opplist=[
        SELECT
            Product2Id, Product2.Name, 
            Opportunity.stagename, OpportunityId, Opportunity.pricebook2Id 
        FROM OpportunityLineItem 
        WHERE Opportunityid IN :Trigger.newMap.keyset() 
        AND pricebookentry.isactive = false 
        AND opportunity.stagename = 'Closed/Won' 
        AND Incremental_AOV__c > 0
        AND Incremental_AOV__c <> null
    ];

    for(OpportunityLineItem oppline : opplist)
    {
        for(Opportunity opp : trigger.new)
        {
            if(!opplist.isempty() || opplist.size() > 0)
            {

                if(opp.pricebook2Id == oppline.opportunity.pricebook2Id && opp.id ==oppline.opportunityid)
                {
                    opp.stagename.addError('LineItem'+ ' '+ '(' + oppline.product2.name + ')'+ ' ' + 'is not an active product for pricebook. Hit Cancel and remove the line item.');
                }
            }
        }
    }
}
4
  • 2
    It will be helpful if you post the trigger code. Commented Oct 1, 2015 at 20:56
  • Apologize. It would be helpful. Code:
    – sfdev09
    Commented Oct 1, 2015 at 21:28
  • Don't put it in the comments...put it in your question. Also you didn't add any.
    – Adrian Larson
    Commented Oct 1, 2015 at 21:28
  • How are you closing the Opportunity records? If it is through a workflow or some other field update, you may be hitting order of execution issues.
    – Adrian Larson
    Commented Oct 1, 2015 at 22:02

1 Answer 1

0

I reworked the trigger for you, let me know if it works.......

If it does not add some debug lines in the trigger to test for values to see if they are as expected and get back with us

Also, inspect the debug log and ensure that the query for the oLI is actually returning data. You have a lot of conditions and maybe they are not being met....

trigger OpportunityValidationonClosed_Won on Opportunity (before update) {

    Map<ID,List<OpportunityLineItem>> opplist= New Map<ID,OpportunityLineItem[]>()

    for( OpportunityLineItem oli : [
                                    SELECT
                                        Product2Id, Product2.Name, 
                                        Opportunity.stagename, OpportunityId, Opportunity.pricebook2Id 
                                    FROM OpportunityLineItem 
                                    WHERE Opportunityid IN :Trigger.newMap.keyset() 
                                    AND pricebookentry.isactive = false 
                                    AND opportunity.stagename = 'Closed/Won' 
                                    AND Incremental_AOV__c > 0
                                    AND Incremental_AOV__c <> null
                                ]){

        if(!oppList.containsKey(oli.OpportunityID)){
            oppList.put(oli.OpportunityID,New OpportunityLineItem[]{oli});
        }else{
            oppList.get(oli.OpportunityID).add(oli);
        }
    }


        for(Opportunity opp : trigger.new)
        {
            if(oppList.containsKey(opp.id)
            {
                for(OpportunityLineItem oppLine : oppList.get(opp.id)){
                    opp.stagename.addError('LineItem'+ ' '+ '(' + oppline.product2.name + ')'+ ' ' + 'is not an active product for pricebook. Hit Cancel and remove the line item.');
                }
            }
        }
    }
}
2
  • Thanks. The OpporunityLineItem list query was looking for stage = Close/Won. I removed from the query and added that criteria to code to determine if triggered record was in that condition and now works.
    – sfdev09
    Commented Oct 2, 2015 at 16:07
  • @sfdev09 - Great, please mark as answer if it helped solve your issue so other with a similar question can find questions with accepted answers
    – Eric
    Commented Oct 2, 2015 at 16:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .