Trigger Definition

Apex trigger is a action which is used to perform some action while saving records to the database in Salesforce. It works at database level where no human interaction is required.

for example: If end user enter Opportunity Amount as $5000 and you have another field on Opportunity called $5000 above opportunity checkbox, you can make that checkbox true or false using Trigger. we will write this trigger later in this section.

Apex Trigger Type

A Trigger simply run in following conditions.

  1. Before insert
  2. Before update
  3. Before delete
  4. After insert
  5. After update
  6. After delete

Where to write your first Apex Trigger.

A Trigger can be written in Develop Menu section from Setup Menu as shown below.

introduction to apex trigger

 

Trigger Context Variable

There are different context variable available inside trigger which can be used to work on records which we get while trigger execute.

 

Variable Usage
isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface,Apex, or the API.
isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface,Apex, or the API.
isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface,Apex, or the API.
isBefore Returns true if this trigger was fired before any record was saved.
isAfter Returns true if this trigger was fired after all records were saved.
isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
new Returns a list of the new versions of the sObject records.

Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

newMap A map of IDs to the new versions of the sObject records.

Note that this map is only available in before update, after insert, and after updatetriggers.

old Returns a list of the old versions of the sObject records.

Note that this sObject list is only available in update and delete triggers.

oldMap A map of IDs to the old versions of the sObject records.

Note that this map is only available in update and delete triggers.

size The total number of records in a trigger invocation, both old and new.

How to write your first Apex Trigger

Any trigger inside Salesforce can be written with following syntax.

Trigger triggername on objectname(event name)

So for example if we are going to write a trigger on opportunity which will update Large_Opp__c field. we will write following code.

Trigger opptrigger on opportunity(before insert)

{
   //Loop Through All the records which are inside Trigger.new
   for(Opportunity opp : Trigger.new)
   {
      //logic to check value
      if(opp.amount>=5000)
      {
         //update value
         opp.Large_Opp__c = true;
      }
   }
}

As you can see I am updating opp larse_opp__c field value on before insert in above trigger, we can update any value prior to save to the database.

Ref:

Salesforce Trigger

Happy Coding!!!