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
HARISH S 11HARISH S 11 

old opportunity amount less than new opportunity amount

Hi Guys,

Am new to salesforece and thisis my fist trigger. So my requirement is if am trying to update an opportunity record and if the new amoutn is less tha the old amount I should get and error  else it should alow me to update the record.

I tried the belwo but doesn't seem to work, Could somebody please help me?

trigger old_new_amount_check on Opportunity (before update) {

        For (opportunity a: Trigger.New){
        
        Opportunity oldAmount = Trigger.oldMap.get(a.Amount);
                if(a.Amount<a.oldAmount){
                    
                    a.addError('New Amount is less that old AMount');
                    }
                    else{
                    
                    System.debug('Opportunity AMount Updated');
                    
                    }
                    
               }     
        
}
Best Answer chosen by HARISH S 11
PawanKumarPawanKumar
Please try below code and let me know If it helps you.

trigger old_new_amount_check on Opportunity(before update) {

 For(opportunity a: Trigger.New) {

  Opportunity oldOpty = Trigger.oldMap.get(a.Id);
  if (a.Amount < oldOpty.Amount ) {

   a.addError('New Amount is less that old AMount');
  } else {

   System.debug('Opportunity AMount Updated');

  }

 }

}

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
Please try below code and let me know If it helps you.

trigger old_new_amount_check on Opportunity(before update) {

 For(opportunity a: Trigger.New) {

  Opportunity oldOpty = Trigger.oldMap.get(a.Id);
  if (a.Amount < oldOpty.Amount ) {

   a.addError('New Amount is less that old AMount');
  } else {

   System.debug('Opportunity AMount Updated');

  }

 }

}

Regards,
Pawan Kumar
This was selected as the best answer
HARISH S 11HARISH S 11
Perfect Pawan! It is now working.

Thanks a lot.