Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 888
Mustafa Turab Ali Avatar asked Apr 05 '11 07:04

Mustafa Turab Ali


People also ask

Does trigger new contain all fields?

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.

How do you update a field in Apex trigger?

Apex Trigger explained with an example Below are the steps to be followed: Create a field in 'Account' with label 'Field Update' and data type as 'Checkbox' Now create a trigger on Contact. Navigate to Setup ->Build ->Customize ->Contacts ->Triggers.

How do you know if a value is changed in Apex?

In Oracle Apex, you can check if an item value changed using the JavaScript API method isChanged() .

How do I know if my trigger is running apex?

You can also view all triggers in Setup by entering Apex Triggers in the Quick Find box, then selecting Apex Triggers. You can add, edit, or delete Apex using the Salesforce user interface only in a Developer Edition organization, a Salesforce Enterprise Edition trial organization, or sandbox organization.


1 Answers

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
        }
    }
}
like image 71
mmix Avatar answered Oct 03 '22 19:10

mmix