+ Start a Discussion
nagamalli tadikondanagamalli tadikonda 

regarding triggers doubt

what is difference  before and after triggers ? when we write after and before triggers ?
Best Answer chosen by nagamalli tadikonda
Anilkumar KotaAnilkumar Kota

Hi Nagamalli,

BEFORE triggers should be used in below scenarios

  1. Custom validation checks in the same object
  2. Update the same record/object
  3. Setting a default values

AFTER triggers should be used in below scenarios

  1. If we need to use Record's ID
  2. Inserting or Updating the related records
  3. To send notification email post commit
For more information refer below links .
http://www.sfdc99.com/2014/01/25/use-vs-triggers/

https://hisrinu.wordpress.com/2011/05/17/difference-between-before-trigger-and-after-trigger/




 

All Answers

Tejas KardileTejas Kardile
Hi,

Please go through below blog for requierd information:
https://hisrinu.wordpress.com/2011/05/17/difference-between-before-trigger-and-after-trigger/

Thanks
EldonEldon
Hi 

When you save a record (insert / update) Salesforce fires a DML statement to its database, internally.
All the code written in the "before update" triggers, executes BEFORE that DML is committed to Salesforce Database. Code written in after trigger executes AFTER the commit is made.
Hence if you are trying to update any value in the record, on which the trigger is fired, you need not write an update call, specifically. But you need to do it in after triggers.
example, if a before trigger is written on Account and you want to change a value of one of the fields it would be
 
for(Account acc: Trigger.new)
{
if(acc.value__c='Temp')
    acc.Value__c = 'New value';
}

but if you will write this on after update you can't update the same field because data is already committed to database so value will be read only.
After update trigger generally works when you want to update any other object. for example if you want to create a new task on account insertion or update you can write logic for that.
 
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;
}

The specific use case of an after trigger is when you need to associate any record to a record being created in your trigger. Here’s an example:
 
trigger AccOpp on Account(after insert) {
  List<Opportunity> newOpps = new List<Opportunity>();
  for (Account acc : Trigger.new) {
    Opportunity opp = new Opportunity();
    opp.Name        = acc.Name + ' Opportunity';
    opp.StageName   = 'Prospecting';
    opp.CloseDate   = Date.today() + 90;
    opp.AccountId   = acc.Id; // Use the trigger record's ID
    newOpps.add(opp);
  }
  insert newOpps;
}

Let me know if you have any issues.
Close this thread by marking it as best answer if it cleared your doubts.

Regards
Anilkumar KotaAnilkumar Kota

Hi Nagamalli,

BEFORE triggers should be used in below scenarios

  1. Custom validation checks in the same object
  2. Update the same record/object
  3. Setting a default values

AFTER triggers should be used in below scenarios

  1. If we need to use Record's ID
  2. Inserting or Updating the related records
  3. To send notification email post commit
For more information refer below links .
http://www.sfdc99.com/2014/01/25/use-vs-triggers/

https://hisrinu.wordpress.com/2011/05/17/difference-between-before-trigger-and-after-trigger/




 
This was selected as the best answer