function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zabi noorZabi noor 

Don't allow to modify the Opportunity object when stage is Closed WonWith this apex trigger i'm not to modify the record if the stage is equal to Closed Won, but when record is not closed won. Not able to change stage to closed won also.

With this apex trigger i'm not to modify the record if the stage is equal to Closed Won, but when record is not closed won. Not able to change stage to closed won also. 


trigger OpportunityTrigger on Opportunity(before update, before delete){
   
    
   
for(Opportunity opp1: Trigger.new) {
    if(opp1.StageName == 'Closed Won'){
        opp1.addError('You cannot modified the opportunity when stage is Closed Won.');                    
        
    }
}
Abdul KhatriAbdul Khatri
Hi Zabi,

I think you may be looking something like this
 
trigger OpportunityTrigger on Opportunity (before update) {
    
    for(Opportunity opp1: Trigger.new) {
        if(opp1.StageName != oldMap.get(opp1.Id).StageName && oldMap.get(opp1.Id).StageName == 'Closed Won'){
            opp1.addError('You cannot modified the opportunity when stage is Closed Won.');                                
        }
    }    
}

Let me know if this helped