Development questions
Why can't we perform DML operations on records in Trigger.new inside a before trigger?
You don't need to, and you shouldn't. In a before trigger, changes you make directly to fields of Trigger.new records are automatically saved as part of the original DML. An explicit update Trigger.new is redundant and errors or causes recursion: for before insert records have no Id yet so the update fails, and for before update it re-fires triggers, risking the depth limit and wasting DML statements.
apex
for (Account a : Trigger.new) {
a.Description = 'Updated'; // saved automatically
}
apex-triggertriggerdml
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?