What is an Apex trigger?
A trigger is the piece of code that executed before and after a record is Inserted/Updated/Deleted from the force.com database. Apex can be invoked through the use of triggers. A Trigger is a functional action which gets on particular events. Triggers will happen before records entering into the database and while goint out of the database. Each event is the firing point.
- Onclick
- Submit.
A Trigger is Apex code that execute before or after the following types of operations.
- Insert
- Update
- Delete
- Undelete
We can have a trigger run before an Object’s records are inserted into the database, after records have been deleted, or even after a record is restored from the Recycle BIN.
Syntax:
Trigger <trigger name> on <Object name> (trigger Events) {
// Implement the Logic here
}
Triggers can be divided into two types.
Types of Triggers:
– Before Triggers
– After Triggers
Before Trigger: Before triggers are used to perform the logic on the same object and specifically we cannot use the DML operation (Insert, update, delete) on these triggers. These triggers fired before the data saved into the database.
After Trigger: After triggers are used to perform the logic on the related objects and these triggers are used access the fields values that are created by system (Ex: CreatedBy, LasteModifiedBy , Record Id etc..).
Bulk Triggers: By default, every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. For each batch of 200 records.
Trigger Context Variables:
All the trigger context variables prefixed with “Trigger.” (Ex: Trigger.isInsert, etc..)
- isInsert: Returns true if the trigger fired due to insert operation
- isUpdate: Returns true if the trigger fired due to the update operation.
- isDelete: Returns true if the trigger fired due to delete operation.
- isBefore: Returns true if the trigger fired before the record saved.
- isAfter: Returns true if the trigger fired after the record saved.
- New: Returns a list of the new version of sObject records.
- Old: Returns a list of an old version of sObject records.
- NewMap: Returns a map of a new version of sObject records. (map stored in the form of map<id,newRecords>)
- OldMap: Returns a map of an old version of sObject records. (map stored in the form of map<id, oldRecords>)
- Size: Returns an integer (total number of records invoked due to trigger invocation for the both old and new)
- isExecuting: Returns true if the current apex code is a trigger.
The below table tells about the events we can use in the new trigger and old trigger
Trigger Event | Trigger.New | Trigger.Old |
Before Insert | Yes | No |
Before Update | Yes | Yes |
Before Delete | No | Yes |
After UnDelete | No | Yes |
After Insert | Yes | No |
After Update | Yes | Yes |
After Delete | No | Yes |
Trigger Context Variable considerations:
– Trigger.Old is always read-only
– We cannot delete trigger.new
– In before triggers, trigger.new can be used to update the fields on the same object.
– In After trigger, we get a runtime exception when the user tries to modify the fields in the same object.
It should be after undelete in the table instead of before undelete
Absolutely right
Here is the asssingment. Pls do let me know your reviews.
Thanks,
The following is your assignment. (Do it in classic).
World Series is taking up ownership of all major sporting events (Cricket, Football, Tennis, Basketball, Baseball
Hockey) that are played all over the world. They would like to have different kinds of tournaments, listed
under each sporting event. Tournaments can only exist, if the sporting event exists. Once a sport has been
removed by World Series, all it’s corresponding tournaments should cease to exist.
Within each tournament, the World Series would like to have the different set of Rules. Within a sport,
two tournaments could have different set of rules. They would like this, along with the teams that participate
in the tournament. For example, a club level tournament will have different teams, as compared to a country
level tournament.
Like tournaments, rules and teams should only exist, as long as the sporting event is there.
They would also like to know which country plays which sports, and which tournament. A country might still
be playing another sport, even if one sporting event is discontinued (removed) by World Series.
Few Rules to take into consideration:
A country cannot exist twice.
You cannot modify the country name once its created.
A team cannot play both country and club level tournaments.
A team can play different tournaments, at the same level.
Each sporting event, needs to have an owner.
A person cannot be the owner of two sporting events.
Each tournament, needs to have an owner.
A person cannot be the owner of two tournaments.
Owner of a tournament cannot be the owner of a sporting event and vice versa.
Design a data model for this, for users to use.
User should not be able to violate any rules.