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
AshwAshw 

how to put this code in class and call it from trigger

if in Opportunity stagename is closed won & closed lost and amount is already exist and then it should not be editable.

trigger OppTrg on Opportunity (before update) {

Opportunity newopp = trigger.new[0];
Opportunity oldOpp = trigger.old[0];

if(oldOpp.StageName == 'Closed Won' || oldOpp.StageName == 'Closed Lost'){
  if(oldOpp.Amount != newOpp.Amount){
   newopp.addError('YOu cannot change the amount when Opp is set to Closed Won or Closed lost');
  }

}

}
Phillip SouthernPhillip Southern
Hi, so here is an approach to put that code into a class and bulkify it:  (please excuse typos)

*****trigger******
trigger OppTrg on Opportunity (Before Update)
{
     oppTrgClass.StageAmountCheck(trigger.new, trigger.oldmap);
}


*****class******
public class oppTrgClass
{
     public void StageAmountCheck(List<Opportunity> opps, Map<Id,Opportunity> oldmap)
     {
          for(Opportunity o : opps)
          {
                 if((oldmap.get(o.Id).stagename == 'Closed Won' || oldmap.get(o.Id).stagename == 'Closed Lost') && o.Amount != oldmap.get(o.Id).Amount)
                 {
                        o.addError('You cannot change the amount when Opp is set to Closed Won or Closed lost');
                  }
          }
     }
}