Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire a update trigger when particular field is updated

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

like image 790
user1048080 Avatar asked Dec 29 '11 18:12

user1048080


1 Answers

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
    }
}
like image 198
Jeremy Ross Avatar answered Sep 27 '22 22:09

Jeremy Ross