When to use triggers in Salesforce

Triggers are used to perform immediate actions based on the previous action.

If declarative workflows or Process Builders don’t meet your specific needs, then it’s time to turn to a programmatic approach using Apex, Force.com’s procedural language. Apex, very similar to Java, is akin to the procedural languages most database systems have to build stored procedures, database triggers, and unit tests that implement reliable, database-centralized, complex business logic. Here’s a quick look at some examples of how you can use Apex with Force.com.

Example: field update.

     A trigger is Apex code that executes before or after specific Data Manipulation Language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted.

Syntax:

trigger triggerName on ObjectName (trigger_events)
{
    /*------------
    code_block
    -------------*/
}

where trigger_events can be a comma-separated list of one or more of the following events:
  1. before insert
  2. before update
  3. before delete
  4. after insert
  5. after update
  6. after delete
  7. after undelete

No comments:

Post a Comment