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) {
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two main types of triggers?

A

before triggers

after triggers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the seven Trigger Events?

A
  1. before insert,
  2. after insert,
  3. before update,
  4. after update,
  5. before delete,
  6. after delete,
  7. after undelete
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are merge and upsert a combination of?

A
  • merge - update + delete
  • upsert - insert + update
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly