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.');
}
}
}
}
}
Opportunity
records? If it is through a workflow or some other field update, you may be hitting order of execution issues.