+ Start a Discussion
JMThorntonJMThornton 

Trigger fire only on certain fields of an Object?

Hello everyone,

 

I'm trying to write a trigger that will only fire when a particular field is updated on an object. Not when any other field is modified. is something like this possible in APEX?

 

Any help is greatly appreciated, thanks!

-Jon

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

The trigger will fire no matter what during an update or insert, but you can check to see if the value changed in the field and process when the field you want changed.

 

 

trigger AccountTrigger on Account(before update){
  
  for(Account a : trigger.new){

     if(trigger.newMap.get(a.Id).Name != trigger.oldMap.get(a.Id).Name){
       //do some stuff
     } 
  }


}

 

 

All Answers

mikefmikef

The trigger will fire no matter what during an update or insert, but you can check to see if the value changed in the field and process when the field you want changed.

 

 

trigger AccountTrigger on Account(before update){
  
  for(Account a : trigger.new){

     if(trigger.newMap.get(a.Id).Name != trigger.oldMap.get(a.Id).Name){
       //do some stuff
     } 
  }


}

 

 

This was selected as the best answer
JMThorntonJMThornton

Ah yes, this should work. Many thanks!