Trigger is an apex code which execute before and after a record is Inserted/Updated/Deleted from salesforce database.
Trigger Syntax:
Trigger <trigger_name> on <Object_Name> (trigger Events) {
// Logic here
}
Trigger Types:
1) Before Trigger
- It is used to perform the DML (Insert/Update/Delete) on same object and data validation functionality.
- It executes before data commit to database.
2) After Trigger
- It is used to perform the DML (Insert/Update/Delete) on cross object.
- It executes after data commit to database.
Note: In after trigger, we get "run time exception" whenever the user tries to modify any field record of the same object.
DML (Data Manipulation Language):
1) Insert
2) Update
3) Delete
4) Undelete
Trigger events:
1) for Insert
before insert
after insert
2) for Update
before update
after update
3) for Delete
before delete
after delete
4) for Undelete
after undelete
Trigger Context Variable:
Trigger context variable are used when you want to use different logic at different events.
1) isInsert: Returns true if this trigger was fired due to an insert operation.
2) isUpdate: Returns true if this trigger was fired due to an update operation.
3) isDelete: Returns true if this trigger was fired due to a delete operation.
4) isBefore: Returns true if this trigger was fired before any record was saved.
5) isAfter: Returns true if this trigger was fired after all records were saved.
6) New: Returns a list of the new versions of the sObject records.
7) Old: Returns a list of the old versions of the sObject records.
8) NewMap: Returns a map of IDs to the new versions of the sObject records. (map<id, newRecords>)
9) OldMap: Returns a map of IDs to the old versions of the sObject records.(map<id, oldRecords>)
10) isExecuting: Returns true if the current context for the Apex code is a trigger.
11) Size: The total number of records in a trigger invocation, both old and new.
Trigger Syntax:
Trigger <trigger_name> on <Object_Name> (trigger Events) {
// Logic here
}
Trigger Types:
1) Before Trigger
- It is used to perform the DML (Insert/Update/Delete) on same object and data validation functionality.
- It executes before data commit to database.
2) After Trigger
- It is used to perform the DML (Insert/Update/Delete) on cross object.
- It executes after data commit to database.
Note: In after trigger, we get "run time exception" whenever the user tries to modify any field record of the same object.
1) Insert
2) Update
3) Delete
4) Undelete
Trigger events:
1) for Insert
before insert
after insert
2) for Update
before update
after update
3) for Delete
before delete
after delete
4) for Undelete
after undelete
Trigger Context Variable:
Trigger context variable are used when you want to use different logic at different events.
1) isInsert: Returns true if this trigger was fired due to an insert operation.
2) isUpdate: Returns true if this trigger was fired due to an update operation.
3) isDelete: Returns true if this trigger was fired due to a delete operation.
4) isBefore: Returns true if this trigger was fired before any record was saved.
5) isAfter: Returns true if this trigger was fired after all records were saved.
6) New: Returns a list of the new versions of the sObject records.
7) Old: Returns a list of the old versions of the sObject records.
8) NewMap: Returns a map of IDs to the new versions of the sObject records. (map<id, newRecords>)
9) OldMap: Returns a map of IDs to the old versions of the sObject records.(map<id, oldRecords>)
10) isExecuting: Returns true if the current context for the Apex code is a trigger.
11) Size: The total number of records in a trigger invocation, both old and new.
Note: All trigger context variables are written as Trigger.contextVariable such as Trigger.isInsert, etc...
Trigger Events using Context variables:
1) for Insert
before insert: Trigger.New
after insert: Trigger.New
2) for Update
before update: Trigger.New and Trigger.Old
after update: Trigger.New and Trigger.Old
3) for Delete
before delete: Trigger.Old
after delete: Trigger.Old
4) for Undelete
after undelete: Trigger.New
1) for Insert
before insert: Trigger.New
after insert: Trigger.New
2) for Update
before update: Trigger.New and Trigger.Old
after update: Trigger.New and Trigger.Old
3) for Delete
before delete: Trigger.Old
after delete: Trigger.Old
4) for Undelete
after undelete: Trigger.New
No comments:
Post a Comment