Salesforce Apex Triggers - How to check if field is included in update trigger?

Mustafa Turab Ali picture Mustafa Turab Ali · Apr 5, 2011 · Viewed 19.3k times · Source

I would really appreciate if someone can guide me to check if a particular field is included in update call inside a before/after update trigger. Many thanks.

Answer

mmix picture mmix · Apr 5, 2011

All fields are always present in the trigger regardless of whether they are dirty or not, to ascertain if a specific field has been modified you have to retrieve a previous version of the row using oldMap map which is a Map<ID, sObject> and compare the values in old and new. For example

trigger CaseOnParticularFieldUpdate on Case (before update) {
    for (Case c: Trigger.new) {
        Case oldCase = Trigger.oldMap.get(c.ID);
        if (c.Field != oldCase.Field) {
            // field was updated, do some magic here
        }
    }
}