11

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Mustafa Turab Ali
  • 111
  • 1
  • 1
  • 3

2 Answers2

15

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
        }
    }
}
Jacob Hacker
  • 1,501
  • 1
  • 13
  • 16
mmix
  • 6,057
  • 3
  • 39
  • 65
  • thanks I am aware of how to check if field data is modified. my question however is related to this scenario. List aclist = [select id, name, email__c from Account]; for(Account a : aclist) { a.email__c = a.name+'@gmail.com'; } update aclist; now what would happen when this trigger runs: trigger on Account (before update) { for(Account a : trigger.new) { if(a.accountNumber==null) { a.accountnumber='NA'; } } } will the accountnumber fields for accounts being update will be changed? – Mustafa Turab Ali Apr 06 '11 at 13:27
  • 1
    Yes. As I said, trigger includes ALL sObject fields. Its not like the visualforce/apex extension which only preloads fields referenced in VF. – mmix Apr 06 '11 at 19:29
1

Trigger will include all fields of that sobject for which it is invoked. You can check previous(old) value and current(new) value of any field in that object and can compare it and can do the operation accordingly.

Hope it helps you.

Promila
  • 26
  • 1