Activity Forums Salesforce® Discussions How can we write a trigger for before and after DML operation?

  • Gourav

    Member
    August 26, 2016 at 10:14 am

    Apex can be invoked by using triggers. Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions.
    A trigger is Apex code that executes before or after the following types of operations:

    • insert
    • update
    • delete
    • merge
    • upsert
    • undelete

    For example, you can have a trigger run before an object's records are inserted into the database, after records have been deleted, or even after a record is restored from the Recycle Bin.

    You can define triggers for top-level standard objects that support triggers, such as a Contact or an Account, some standard child objects, such as a CaseComment, and custom objects. To define a trigger, from the object management settings for the object whose triggers you want to access, go to Triggers.

    There are two types of triggers:

    • Before triggers are used to update or validate record values before they’re saved to the database.
    • After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDatefield), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.

    Sample Trigger:-

    trigger <Trigger Name> on <sobject> (<events>) {

    if(Trigger.<event>){

    //Your code logic

    }else if(Trigger.<event>){

    //Your code logic

    }
    }

  • shariq

    Member
    August 7, 2017 at 12:52 pm

    Hi Mohit,

    Before triggers can be used to update or validate record values before they are saved to the database.

    After triggers can be used to access field values that are set by the database (such as a record's Id or lastUpdated field) and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue.

    Use Before Trigger:
    In the case of validation check in the same object.
    Insert or update the same object.

    Use After Trigger:
    Insert/Update related object, not the same object.
    Notification email.
    We cannot use After trigger if we want to update a record because it causes read only error. This is because, after inserting or updating, we cannot update a record.

    trigger AccountTrigger on Account (before insert, after insert)
    {
    if(Trigger.isBefore)
    {
    if(Trigger.IsInsert)
    {
    Contact cont = new Contact();
    cont.LastName = Trigger.new[0].name;
    cont.FirstName = Trigger.new[0].name;
    insert cont;
    }
    else if(Trigger.IsInsert&&Trigger.IsAfter)
    {
    Contact cont = new Contact();
    cont.LastName = '(Updated)';
    cont.FirstName = Trigger.new[0].name+ 'Updated';
    insert cont;
    }
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos