What Are the Apex Triggers in Salesforce?

Apex Triggers:
         Trigger is an apex code that gets executed whenever a specific DML operation is performed on an Object record. It allow developers to programmatically check for duplicate records, update fields across objects, automate the creation of new records based on criteria, and much more.
Apex_Triggers

For example: If I wanted to check for duplicity on the user email field every time a user was created or updated I would write a trigger and assign the "before insert, before update" operations to it. This would allow me to query the database and throw an exception if a duplicate is found before saving to the database.

Triggers can be primarily classified into two types:
  • Before trigger 
  • After trigger
Also Read: What is Triggers Execution Order?
Before Triggers:
               Before triggers are ones that get executed before the record is saved to the database.

After Triggers:
            After triggers are ones that get executed after the record is saved to the database. Each one has its own significance as each one has to be used in specific scenarios.

Usage of Before Triggers:
  • Can be used to perform validations and stop the creation / updation / deletion of records if they are not satisfying the criteria. 
  • Data can be modified in before triggers only, hence record field auto population by the system can be performed here.
    Usage of After Triggers:
    • By the time these triggers execute the record ID and the system generated fields such as created by, modified by etc… are generated by the system and these fields such as ID can be used to build relationships i.e create relationship records.
    Trigger Syntax:
    trigger <triggerName> on <Object>( DML Events ) {
    /* -- Execute Trigger Logic Here -- */
    }

    DML Events:
            Below are the DML events that can be specified on a trigger.

    Before Insert:
                 Trigger that executes due to an insert operation and before the record is saved to the database it is a before Insert trigger.

    Before Update:
                   Trigger that executes due to an update operation and before the record is saved to the database it is a before update trigger.

    Before Delete:
                Trigger that executes due to a delete operation and before the record is saved to the database it is a before delete trigger.

    After Insert:
               Trigger that executes due to an insert operation but after the record is saved to the database it is an after Insert trigger.

    After Update:
              Trigger that executes due to an update operation and after the record is saved to the database it is an after update trigger.

    After Delete:
               Trigger that executes due to a delete operation and after the record is deleted from the database it is an after delete trigger.

    After Undelete:
               Trigger that executes due to a undelete operation and after the record is undeleted from the database it is an after undelete trigger.

    Trigger Context Variables:
    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 theAPI.)
    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 update triggers.
    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.

    The following table lists considerations about certain actions in different trigger events:
    Trigger Event Can change fields using trigger.new Can update original object using an update DML operation Can delete original object using a delete DML operation
    beforeinsert
    Allowed.
    Not applicable. The original object has not been created; nothing can reference it, so nothing can update it. Not applicable. The original object has not been created; nothing can reference it, so nothing can update it.
    afterinsert Not allowed. A runtime error is thrown, as trigger.newis already saved.
    Allowed.
    Allowed, but unnecessary. The object is deleted immediately after being inserted.
    beforeupdate
    Allowed.
    Not allowed. A runtime error is thrown. Not allowed. A runtime error is thrown.
    afterupdate Not allowed. A runtime error is thrown, as trigger.newis already saved. Allowed. Even though bad code could cause an infinite recursion doing this incorrectly, the error would be found by the governor limits. Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible.
    beforedelete Not allowed. A runtime error is thrown. trigger.new is not available in before delete triggers. Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible. Not allowed. A runtime error is thrown. The deletion is already in progress.
    afterdelete Not allowed. A runtime error is thrown. trigger.new is not available in after delete triggers. Not applicable. The object has already been deleted. Not applicable. The object has already been deleted.
    afterundelete Not allowed. A runtime error is thrown. trigger.old is not available in after undelete triggers.
    Allowed.
    Allowed, but unnecessary. The object is deleted immediately after being inserted.

    COMMENTS

    BLOGGER: 2
    Loading...
    Name

    Apex Apex Default Methods Apex Methods Apex_Collections Apps Batch Apex Books CRM eBooks Data Import Wizard Data Management DataLoader Dataloader.io Difference Between Error in Salesforce FAQ's FAQ's-Apex FAQ's-Apps FAQ's-CRM FAQ's-Data loader FAQ's-SOQL & SOSL FAQ'S-Triggers FAQ's-User Profile & Security FAQ's-VF Force.com Explorer Force.com Ide Formulas & Functions Integration Interview Questions Only Latest_Updates Limits&Best Practices Online-Training Reports and Dashboards Salesforce Deployment Salesforce Realtime Examples On Development Salesforce Realtime Task On Admin Salesforce Realtime Tasks Salesforce Realtime Tasks- Apex Salesforce Training Salesforce1 Mobile Sites SOQL Spring'14 Release Triggers User Profile & Security VF Tags VisualForce Winter'14 Release Wizard for Accounts/Contacts Workbench Workflows and Approvals
    false
    ltr
    item
    Sfdc Gurukul- All in one place for salesforce and force.com step by step tutorial for beginners: What Are the Apex Triggers in Salesforce?
    What Are the Apex Triggers in Salesforce?
    What Are the Apex Triggers in Salesforce?,How to write a triggers in Salesforce,before and after triggers in Salesforce
    http://4.bp.blogspot.com/-1PZhFg0mt8A/UzrQ4bysXXI/AAAAAAAABq0/bxE0cIz8bM0/s1600/Apex_Triggers.jpg
    http://4.bp.blogspot.com/-1PZhFg0mt8A/UzrQ4bysXXI/AAAAAAAABq0/bxE0cIz8bM0/s72-c/Apex_Triggers.jpg
    Sfdc Gurukul- All in one place for salesforce and force.com step by step tutorial for beginners
    http://sfdcgurukul.blogspot.com/2013/07/what-are-apex-triggers-in-salesforce.html
    http://sfdcgurukul.blogspot.com/
    http://sfdcgurukul.blogspot.com/
    http://sfdcgurukul.blogspot.com/2013/07/what-are-apex-triggers-in-salesforce.html
    true
    4199533888133360731
    UTF-8
    Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy