Salesforce Menu

Triggers interview Question

1. What are the trigger events?
Before Mode: Before the record is saving into the database, it will fire.

After Mode: After the record is saved into the database (doesn’t commit at this point of time), it will fire.

before

after

before insert

after insert

before update

after update

before delete

after delete

after undelete

2. What are the trigger context variables?
To capture the runtime information we use trigger context variables.

Below context variables will return either true or false.
 Trigger.isBefore (returns true if the trigger context is Before Mode)
 Trigger.isAfter (returns true if the trigger context is After Mode)
 Trigger.isInsert (returns true if the trigger context is Insert)
 Trigger.isUpdate (returns true if the trigger context is Update)
 Trigger.isDelete (returns true if the trigger context is Delete)
 Trigger.isUndelete (returns true if the trigger context is Undelete)
 Trigger.isExecuting (returns true if the apex class method is getting call from Apex Trigger)

Below context variables will store records at runtime.
 trigger.old (stores history (old versions) of the records.)
 trigger.oldMap (stores history (old versions) of the records along with id.)
 trigger.new (stores new version of the records.)
 trigger.newMap (stores new version of the records along with id.)

3. Availability of trigger.old and trigger.new for the different trigger events?
Apex Trigger Collections availability for the different events –

Events trigger.old trigger.oldMap trigger.new trigger.newMap
before insert
after insert
before update
after update
before delete
after delete
after undelete

4. Read/Write accessibility of trigger.old and trigger.new?

Events trigger.old trigger.oldMap trigger.new trigger.newMap
before insert NA NA Read/Write NA
after insert NA NA Read Only Read Only
before update Read Only Read Only Read/Write Read Only
after update Read Only Read Only Read Only Read Only
before delete Read Only Read Only Read Only Read Only
after delete Read Only Read Only Read Only Read Only
after undelete Read Only Read Only Read Only Read Only

5. When to use before triggers and when to use after triggers?

Before Triggers
To perform the validations we should use before triggers.
If you are updating any field on the same object on which you are writing the trigger and no need to explicitly include the DML statemetns (already due to DML operation only trigger fire and it is still in progress at this point of time.)

After Triggers
If you are dealing with relationship records and if you need record id in these situations we should use after trigger (in before insert record doesn’t contain the record id).

6. For the same event if there are multiple triggers on the object, how to control the order of execution?

We cannot control the order of execution in this situation. It is recommended to have only one trigger per one object.
Note: We can keep the logic of the apex trigger in an apex class and can invoke from that class.

7. What are the recursive triggers and how to avoid?
If we perform update operation on the record in after update event logic recursive triggers will arise.

Using static boolean variable in an apex class (we should not keep static boolean variable inside of the trigger) we can avoid recursive triggers.

8. What is MIXED-DML-OPERATION error and how to avoid?
If we perform DML operation on standard/custom object and global objects(User, UserRole, Group, GroupMember, Permission Set, etc…) in same transaction this error will come.

To avoid this error, we should perform DML operation on standard/custom object records in a different transaction.

In general all the apex classes and apex triggers execute synchronously (execute immediately).

if we perform DML operation on standard/custom object records asynchronously (execute in future context), we can avoid MIXED-DML-OPERATION error.

To execute logic asynchronously keep the logic in an apex method (in a separate apex class, not in same apex trigger) which is decorated with @future annotation.

9. What is the order of execution in salesforce?

We are creating validation rules, workflow rules, assignment rules, auto-responsive rules, escalation ruels and apex triggers etc.

If the condition is same for all the rules which will execute first and which will execute next, for this salesforce provide the order.

Order of execution in salesforce – Order of execution in Salesforce:

  1. Prepare the record for insert/update operation.

  2. It will replace all the old field values with new field values.

  3. If the request is coming form UI all the system validations will execute –

    • DataType

    • Length

    • Required

    • unique

    • pageLayot level validations

  4. before triggers

  5. Custom Validations and again system validation rules will fire (pageLayot level validations won’t fire at this point of time)

  6. record will be saved into the database but doesn’t not commit.

  7. after triggers.

  8. assignment rules.

  9. auto-responsive rules.

  10. Workflow Rules

  11. incase of Workflow Rule Field updates again before triggers and after triggers fire one more time. System validation ruels for duplicate chcek.

  12. Escalatoin Rules.

  13. Rollup-Summary fields will be calculated.

  14. Grant parent Rollup-Summary fields will be calculated.

  15. Criteria base sharing evaluation.

  16. Record will be commited into the database.

  17. Post Commit logic (Sending emails).

Subscribe Now