14

The title pretty much says it all.

20

Depends on your usage. From my experience here's the break down:

Before

  • I'm updating the record that's being updated/inserted - or doing something based on the record being modified
  • Examples: Set value of a pick list based on criteria. Send apex e-mail based on the record updated/inserted

After

  • I'm updating or creating records that are NOT being updated/inserted
  • Examples: Create a task of an Opportunity that's been edited, Change a look up value on a related record from the Opportunity being edited

The main thing to consider is that the Before happens before the data has been written to the server. This means you can modify the records in "Trigger.new" without having to call a separate "Update." This is ideal if you want to modify data in the records within Trigger.new

After happens after the data has been written to the server. This is important when wanting to create additional related records (Can't create a related record until AFTER the parent has been inserted).

I find about 95% of all the triggers I have ever written are Before Update/Insert. It's very rare that I need a trigger that runs AFTER.

  • The trigger that I am creating fits neatly into your 'Before' criteria (I'm changing field values on the record that is being updated/inserted), however, when I actually set the field when using 'Before', nothing happens: finAcct.Wealth_Advisor_Lookup__c = finAcct.client__r.Wealth_Advisor__c; However, when I run this using 'After' (along with an Update finAcct statement) it works fine. Any idea why it's not working as a 'Before' trigger? One note, I am not looping over trigger.new, I'm looping over an SOQL query of those records so I have more related fields available. – jackerman09 Oct 8 '12 at 18:08
  • Jackerman09 - What does the debug logs say? Validation & WF rules run one more time after Before Triggers. So maybe it's being overwritten? Are you updating a lookup field? And is this an Before Update or Before Insert (both?) – Salesforce Wizard Oct 8 '12 at 18:29
  • I am updating a lookup field (and it worked when using an After trigger), it is both an update and insert trigger, thanks again! – jackerman09 Oct 8 '12 at 18:32
  • I think the issue may be stemming from my inclusion of this if-statement, which determines whether or not to change the field's value: if (finAcct.Wealth_Advisor_Lookup__c != finAcct.client__r.Wealth_Advisor__c){ – jackerman09 Oct 8 '12 at 18:38
  • I used a few system.debug's to test it out, and it was not entering the if-statements, however, even without the conditional, the system.debugs show that the value is changing, however, it is not being updated in the database. Thanks again @salesforce wizard – jackerman09 Oct 8 '12 at 18:49
3

Before Trigger: In case of validation check in the same object

After Trigger: Insert/Update related object, not the same object

3

Updating the records being handled by the trigger must be done in the before trigger.

Validating the records being handled by the trigger should ideally be done in the after trigger. At this point no other data changes on the trigger records are admitted (for example by other triggers) - this is the safest point at which to validate the data.

Initiating updates on related records (including chatter posts), sending emails etc., can be done either in before or after triggers. For example:

  • if you need a record id (on an insert) to use in a lookup field in a related object, then this needs to be done in the after trigger
  • if you want to insert a related record and use its id in a lookup field on the trigger record, then this has to be done in the before trigger
  • if you need to use the record autonumber/name (e.g. in an email) then this must be done in the after trigger
  • I actually tend to validate in a before trigger. If a validation is so complex it must be performed with Apex, and you have field updates tinkering with it after the before context...you're going to have a lot of bugs. – Adrian Larson May 6 '15 at 14:54
-1

enter image description hereHere's the gist of When to use which trigger.

Reference: SFDC99.com

  • 2
    I'm afraight it's a bit more complex and scenario dependant. You'd have to take into account the order of execution and logical dependancies on other thing that may change field values. – Samuel De Rycke Apr 23 '15 at 7:30
  • Soooooo, according to this then there is absolutely no reason to ever use a before update??? – Chris Duncombe Apr 23 '15 at 11:00
  • 1
    It's almost 100% to do with whether you're modifying records passed to the trigger or outside of it. Triggers are very frequently justified before update and before delete. (There is no before undelete.) – Adrian Larson Apr 23 '15 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.