Apex Triggers Flashcards
1
Q
What is a Trigger?
A
- a trigger is a set of code is invoked when a set DML operation occurs on a specified object
trigger triggerName on sObject (before insert) {
}
2
Q
What are the two main types of triggers?
A
before triggers
after triggers
3
Q
What is a before trigger?
A
- before triggers are invoked before the Save step in the order of execution
- they’re used for performing more complex validation than we can’t accomplish through validation rules, setting default values for fields, or performing updates on the fields of the records involved in the DML operation
4
Q
What is a after trigger?
A
- after triggers are invoked after the Save step in the order of execution
- in after triggers, we’re not able to edit the fields of the records that invoked the DML operation, so we use after triggers for making changes to other records
5
Q
What are the seven Trigger Events?
A
- before insert,
- after insert,
- before update,
- after update,
- before delete,
- after delete,
- after undelete
6
Q
Boolean Trigger Context Variables
Returns true if…
A
- Trigger.isExecuting
- A trigger is the current execution context.
- Trigger.isInsert
- The trigger was invoked by an insert operation.
- Trigger.isUpdate
- The trigger was invoked by an update operation.
- Trigger.isDelete
- The trigger was invoked by an delete operation.
- Trigger.isUndelete
- The trigger was invoked by an undelete operation.
- Trigger.isBefore
- The trigger is a before trigger - i.e. it was fired before the soft-save in the Order of Execution.
- Trigger.isAfter
- The trigger is a after trigger - i.e. it was fired after the soft-save in the Order of Execution.
7
Q
What are merge and upsert a combination of?
A
- merge - update + delete
- upsert - insert + update
8
Q
What are the best practices for Trigger?
A
- we should have at most one trigger per object
- triggers should be logicless
- triggers should be bulkified
- we should avoid writing recursive triggers
9
Q
What is the addError() method?
A
- if we determine that a record is invalid, we can call the addError() method on it, which prevents the DML operation from occurring on that particular record