I know after trigger cannot update the same record. But i would like to know why. I know the record is committed into the database. So the record is read only. But why the record is read only.
Apex triggers allow you to choose to run code at specific points in a database operation where the semantics are well defined:
- before trigger: before the database has been updated
- after trigger: after the database has been updated
So by design, if the platform allowed modification of the record in an after trigger the change would not be persisted and thrown away.
You can choose whether to use a before or after trigger.
(The design probably comes from the underlying Oracle database.)
In an After Trigger, Trigger.new will contain the the Id's and field values of any records that have been changed by a DML operation.
The values contained in Trigger.new can be compared to Trigger.old in order to determine which records meet the criteria for firing the trigger and need to be operated on. Unlike in a Before Trigger, the values in Trigger.new cannot be changed or edited.