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
Jan Kopejtko 2Jan Kopejtko 2 

Trigger to fire only if field value has been changed

Okay fellas, this one is a toughie.

I have a trigger that is before update on object A. I have a field on the same object which is updated by a process.

I need to somehow make an if statement in the trigger NOT to fire if the value in the field is changed. This is a before update trigger. Is it even possible?
Best Answer chosen by Jan Kopejtko 2
IamSRNIamSRN
Correct me if I am wrong, you want to block the trigger execution if the value in the field is changed. In fact if the value is changed, you want to replace the updated record with old record? If so, just use Trigger oldMap and replace the new data with old one. Below is an example for Contact (if email changed, I am reverting back all field values to old ones)
 
trigger ContactTrigger on Contact (before update) {
    if(Trigger.isBefore && Trigger.isUpdate){
        Map<String, Schema.SObjectField> fieldMap  = Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap();
        for(Contact c: Trigger.new){
            if(Trigger.oldMap.get(c.id).email != null && c.email != Trigger.oldMap.get(c.id).email){
                //email changed. Revert back all changes
                for(String fieldKey: fieldMap.keySet()){
                    if(fieldMap.get(fieldKey).getDescribe().isUpdateable()){
                       c.put(fieldKey, Trigger.oldMap.get(c.id).get(fieldKey));
                   }
                }
            }
        }
    }
}

 

All Answers

Neo NemNeo Nem
can you please let us know what action  exactily makes your process to fire!
IamSRNIamSRN
Correct me if I am wrong, you want to block the trigger execution if the value in the field is changed. In fact if the value is changed, you want to replace the updated record with old record? If so, just use Trigger oldMap and replace the new data with old one. Below is an example for Contact (if email changed, I am reverting back all field values to old ones)
 
trigger ContactTrigger on Contact (before update) {
    if(Trigger.isBefore && Trigger.isUpdate){
        Map<String, Schema.SObjectField> fieldMap  = Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap();
        for(Contact c: Trigger.new){
            if(Trigger.oldMap.get(c.id).email != null && c.email != Trigger.oldMap.get(c.id).email){
                //email changed. Revert back all changes
                for(String fieldKey: fieldMap.keySet()){
                    if(fieldMap.get(fieldKey).getDescribe().isUpdateable()){
                       c.put(fieldKey, Trigger.oldMap.get(c.id).get(fieldKey));
                   }
                }
            }
        }
    }
}

 
This was selected as the best answer
Jan Kopejtko 2Jan Kopejtko 2

Sorry for the delay. Thanks alot and kudos.