Fire a update trigger when particular field is updated

user1048080 picture user1048080 · Dec 29, 2011 · Viewed 12.4k times · Source

How do I fire a before update trigger only one or some particular fields are updated and not fire when other fields are updated as it causes duplicate record in other object

Answer

Jeremy Ross picture Jeremy Ross · Dec 29, 2011

Triggers aren't granular like that. In the before update trigger, use the Trigger.New and Trigger.OldMap variables to compare each new record to see if the fields you're interested in have changed.

for (Opportunity newOpp : Trigger.new) {
    Opportunity oldOpp = Trigger.oldMap.get(newOpp.Id);
    if (oldOpp.Name != newOpp.Name) {
        // do something
    }
}