Saturday, 17 March 2018

APEX TRIGGERS IN SALESFORCE

Apex Triggers:
                        Trigger is an apex code that gets executed automatically when ever an associated DML operations are performed.
-> A trigger is used tp perform either validations or automation's whenever an associated DML operations are performed.

Trigger Context Variables :

Whenever a trigger is invoked/called it has a group of supportive variables which hold runtime information during the current trigger execution and these variables are known as trigger context variables.

                                 |----- Is insert         |
i) DML Statements -|----- Is update       |  ----- Boolean Variables
                                 |----- Is delete        |
                                 |----- Is undelete    |

                                      |---- Is before    |
ii) Trigger execution -- |                         | ------- Boolean variables
                                      |---- Is after       |


                  |--- Old    |------- List Types
iii) Data -- |--- New  |
                  |----- Old Map  |------- Map types
                  |----- New map |

Old :- data before DML operations
New :- data during/after DML operations
Oldmap :- data before DML operations(<id,old record>)              |-But it stores as <key, value> pairs
Newmap :- data during/after DML operations (<id,new record>) |


Trigger Events :- These are the events that will fire the triggers when DML is performed

1. Before Insert
2. Before Update
3. Before Delete
4. After Insert
5. After Update
6. After Delete
7. After Undelete

Syntax : 
         Trigger triggername on sobject ( before event / after event)
         {
          }
Insert : Whenever we performing insert operations we don't have old data, we have only new data/records. so, we will use either "new" or "newmap" context variables.
             Because, they won't be any existing data, so whenever you insert any record, that record will automatically taken into new or newmap. There is no concept of old whenever inserting the records.

Delete : Whenever we performing delete operations we don't have new data, we have only existing old data and then we will perform delete operation on that records.
              So, deleted records will automatically taken into either "old" or "oldmap" context variables.

Update : We can take new, newmap, old , oldmap 
--> Once we update the old data we have new data while update
--> Also while we have a new data the only we have to update old data.

Before : Before triggers will involved mainly with single object and we want to perform any validations/fields updates on the same object then we have to go to before triggers. (before it saving it into the data base).

--> We perform validations before data saving into the database
--> Before triggers will also perform with multiple objects, but if we want to perform validations the we will go only with single object.

After : After triggers will involved mainly with multiple objects and we want to perform any automation's or some operations in other object's then we have to use after triggers.
--> It is mainly used to perform DML operations on other objects or with relational objects.

No comments:

Post a Comment