Difference between Before Trigger and After Trigger

Do you wonder always what exactly is the difference between before and after triggers. Here is the small example which gives you better understanding with hello world example.

  • Before Insert example
  • trigger HelloWorldTrigger on Account (before insert) {
    for(Account a : Trigger.New)
    a.Hello__c = 'World';
    }
    


    For the same example if you just change the event to after insert then you will end up having an error as record has been locked. The work around for this is to allocate memory and update the same record so that you can avoid the record locking issue

  • After Insert example
  • trigger HelloWorldTrigger on Account (after insert) {
        List<Account> accList = new List<Account>();
        for(Account a : Trigger.New){
            Account acc = new Account(id = a.id, Hello__c = 'World');
            accList.add(acc);
        }
        if(accList.size()>0)
        update accList;
    }
    

    Note: Here you have to allocate a memory and then update that record explicitly.

  • Conclusion:
  • If you would like to update the same record then you need to use before trigger however if you would like to work with system generated fields then you have to use the after trigger.

    Here is the standard Salesforce.com order of execution

  • Old record loaded from database (or initialized for new inserts)
  • New record values overwrite old values
  • System Validation Rules
  • All Apex “before” triggers
  • Custom Validation Rules
  • Record saved to database (but not committed)
  • Record reloaded from database
  • All Apex “after” triggers
  • Assignment rules
  • Auto-response rules
  • Workflow rules
  • Escalation rules
  • Parent Rollup Summary Formula value updated (if present)
  • Database commit
  • Post-commit logic (sending email)
  • About these ads
    This entry was posted in Apex, Salesforce Tips & Tricks, Visualforce. Bookmark the permalink.

    16 Responses to Difference between Before Trigger and After Trigger

    1. Gunwant says:

      ya it was good but i couldnot get same field and system generated fields please clear it

    2. arun says:

      what is the difference between trigger.new and trigger.old ?
      some time both gives same results.

      • Srini says:

        Trigger.New and Trigger.old both are trigger context variables. New gives you the updated values where as Old gives you the prior values of the record.
        Trigger.new is available in Before Insert, After Insert, Before Update, After Update where as Trigger.old is available in Before Update, After Update, Before Delete, After Delete.
        For more information look here

        • Neelam says:

          Hi Srinu ..while doing an after insert trigger i am getting an error .The error is Review all error messages below to correct your data.
          Apex trigger myTrigger caused an unexpected exception, contact your administrator: myTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.myTrigger: I did the same after insert Example as shown above .Please send the answer Asap

    3. Jaipal says:

      What are the Differences between Workflows and Triggers???

      • Srini says:

        Workflows fire only at the last part of the execution and it can only work on the same object/ master-detail but not on other objects where as triggers can fire on before/after events of the DML and it can query the objects irrespective of their relationships.

        Try reading about the trigger and their order of execution in apex developers guide, then you will be able to understand the differences.

        Thanks
        Srini

    4. Sangita says:

      Thanks so much for the explanation – it helped a great deal

    5. Ashutosh says:

      Hi Srini…dat link is very useful bt i need to know that how to decide when to write after trigger n when to write before trigger…….As I think if we want to perform operations on the same object then we need to write before trigger…….can u please guide me..

    6. chandra says:

      what is the exact use of Triggers in salesforce?

    7. saikiran says:

      when iam using after insert trigger..some error is coming..
      it is showing compile error like unexpected token:for at line 3 column 0
      please give me solution for this problem

    8. Pingback: Force.com – Triggers | ahaTechMoments

    9. Disha says:

      plz tell me difference between implicit trigger and explicit trigger

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s