Fire a update trigger when particular field is updated
Asked Answered
P

1

6

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

Pugilism answered 29/12, 2011 at 18:3 Comment(0)
B
15

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
    }
}
Bassett answered 29/12, 2011 at 18:9 Comment(2)
Can you give me a small example. I am kind of new to salesforcePugilism
Map<ID,sObject> NewRecords = Trigger.NewMap; salesforce.com/us/developer/docs/apexcode/…Sike

© 2022 - 2024 — McMap. All rights reserved.