reeeval
10/10/2016 - 7:20 AM

Apex trigger validation to prevent stage going to 'Closed Won' or 'Negotiation/Review' if status not 'Approved'

Apex trigger validation to prevent stage going to 'Closed Won' or 'Negotiation/Review' if status not 'Approved'

trigger validateStageByStatusTrigger on Opportunity (before insert, before update) {
    for (Opportunity opp : Trigger.new) {
        if(Trigger.isInsert) {
            if(opp.StageName.equals('Closed Won') || opp.StageName.equals('Negotiation/Review')) {
                if (!opp.Status__c.equals('Approved')) {
                    opp.addError('Cannot proceed to the intended stage, the opportunity must be approved in advance');
                }
            }
        }
        else if(Trigger.isUpdate) {
            if(opp.StageName.equals('Closed Won') || opp.StageName.equals('Negotiation/Review')) {
                if (!opp.Status__c.equals('Approved')) {
                    opp.addError('Cannot proceed to the intended stage, the opportunity must be approved in advance');
                }
            }
        }
    }
}