Salesforce with Ankit

Salesforce beyond the clouds !

Apex Triggers – II

In this article we will continue Apex Triggers and will discuss about trigger context variable and bulkification of triggers. First we will cover all context variables.

  1. Trigger.isExecuting –
    • It returns a boolean value indicating whether or not code is running within trigger context or not.
  2. Trigger.isInsert –
    • It will return a value of true, if the current trigger context is insert trigger.
  3. Trigger.isUpdate –
    • It will return a value of true if current trigger context is update trigger.
  4. Trigger.isDelete –
    • It will return a value of true if current trigger context is delete trigger.
  5. Trigger.isUndelete –
    • It will return a value of true if current trigger context is undelete trigger.
  6. Trigger.isBefore –
    • It will return a value of true if current trigger context is before trigger.
  7. Trigger.isAfter –
    • It will return a value of true if current trigger context is after trigger.
  8. Trigger.new –
    • It returns a list of new versions of records, undergoing the save process, which has data-type List<sObject>.
    • Records within Trigger.new list are only writable in before trigger context.
    • Trigger.new is only available in insert, update, and undelete.
  9. Trigger.newMap –
    • It returns map of type map < Id, sObject >.
    • List generated by Trigger.newMap.values ( ) is same list of records available in Trigger.new.
  10. Trigger.old –
    • It returns a list of records undergoing the save process having data-type List<sObject>.
    • It returns the list of old versions of records in databse, prior to any change being made.
    • It is available in only update & delete.
  11. Trigger.oldMap –
    • It returns map of type map < Id, sObject > where sObjects are records of older version.
  12. Trigger.operationType –
    • It returns an enum that is of type system.TriggerOperation and corresponds to current trigger context.
    • Available enum values are:
      • BEFORE_INSERT
      • AFTER_INSERT
      • BEFORE_UPDATE
      • AFTER_UPDATE
      • BEFORE_DELETE
      • AFTER_DELETE
      • AFTER_UNDELETE
  13. Trigger.size –
    • It returns the number of records that are within current trigger invocation, both old & new.

Now we will discuss about bulkification of triggers .

  1. Salesforce will pass upto 200 records for processing within the trigger. I.e. size of trigger.new can be 200 records./
  2. Bulkification is key consideration whenever we are writing trigger code, it will help to avoid hitting governor limits.
  3. We should try to minimize DML.

Thanks for reading this article till the end. In next article we will discuss about SOQL with some examples.

Published by

Leave a comment