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.
- Trigger.isExecuting –
- It returns a boolean value indicating whether or not code is running within trigger context or not.
- Trigger.isInsert –
- It will return a value of true, if the current trigger context is insert trigger.
- Trigger.isUpdate –
- It will return a value of true if current trigger context is update trigger.
- Trigger.isDelete –
- It will return a value of true if current trigger context is delete trigger.
- Trigger.isUndelete –
- It will return a value of true if current trigger context is undelete trigger.
- Trigger.isBefore –
- It will return a value of true if current trigger context is before trigger.
- Trigger.isAfter –
- It will return a value of true if current trigger context is after trigger.
- 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.
- 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.
- 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.
- Trigger.oldMap –
- It returns map of type map < Id, sObject > where sObjects are records of older version.
- 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
- 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 .
- Salesforce will pass upto 200 records for processing within the trigger. I.e. size of trigger.new can be 200 records./
- Bulkification is key consideration whenever we are writing trigger code, it will help to avoid hitting governor limits.
- 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.
Leave a comment