Apex Triggers in salesforce
A trigger is Apex code that executes before or after the following types of operations:
Trigger triggerName on object(Trigger Event handlers)
Types of Trigger :-
- Before Trigger ( It will be fired before records saving into database)
- After Trigger ( It will be fired after records saving into database but still not committed)
When to use Before-Triggers?
Before trigger events occur before a records changes are committed to the database. This particular event is ideal for performing data validation, setting default values, or performing additional logic or calculations.
Please keep in mind that in the case of before insert events, because the event is executed before the record is committed to the database it will not have a record id.
When to use After-Triggers?
After trigger events occur after a record has been committed to the database, which means that records being inserted will have a record id available.
This particular event is ideal for working with data that is external to the record itself such as referenced objects or creating records based on information from the triggered object.