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.
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
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.
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
ya it was good but i couldnot get same field and system generated fields please clear it
In the before trigger you will not able to get the system generated fields, so in your case you need to use after trigger
system generated fields means what
in before trigger you generate fields that you are seeing like acc name,in after trigger you will update fields like formula field(that system will generate automatically get it)
what is the difference between trigger.new and trigger.old ?
some time both gives same results.
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
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
What are the Differences between Workflows and Triggers???
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
Thanks so much for the explanation – it helped a great deal
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..
You are correct, it’s completely varies based on your requirement 🙂
what is the exact use of Triggers in salesforce?
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
Pingback: Force.com – Triggers | ahaTechMoments
plz tell me difference between implicit trigger and explicit trigger