Contents[Hide]

This is a Salesforce trigger template which allow to run code selectively depended on trigger state.

1. Basic skeleton

Trigger file contins only redirection to methods from handler class.

For before insert trigger use Trgger.new as Trigger.newMap will return nullpointer error because of missing Id.

/**
* @author      Xxxxx Zzzzzzz <This email address is being protected from spambots. You need JavaScript enabled to view it.>
* @since       2013-02-16
* @date        2015-04-25
* @version     v1.1
* Description  Trigger class.
*/

trigger Product_Trigger on Product2 (before insert, before update, before delete, after insert, after update,
    after delete, after undelete) {
       
    if (Trigger.isBefore){
       
        if (Trigger.isInsert){
           Product_TriggerHandler.setCurrencyAsEUR(Trigger.new);
        }

        if (Trigger.isUpdate){

        }

        if (Trigger.isDelete) {

        }
    
    }
    
    if (Trigger.isAfter){
    
        if (Trigger.isInsert){
           
        }
       
        if (Trigger.isUpdate){

        }

        if (Trigger.isDelete) {

        }

        if (Trigger.isUndelete) {

        }
    }

}

2. Handler class

Trigger handler class contains all methods that are executed from trigger.

/**
 * @author      Xxxxx Zzzzzzz <This email address is being protected from spambots. You need JavaScript enabled to view it.>
 * @since       2015-04-27
 * @date        2015-04-27
 * @version     1.0
 * @description Trigger handler.
 */
public with sharing class Product_TriggerHandler {

    public static void setCurrencyAsEUR (List<Product2> triggerNew) {
        for (Product2 p2 : triggerNew) {
            p2.CurrencyIsoCode = 'EUR';
        }
    }


}

3. Bulkified code

You must always take in consideration that date may come in big batches for 200 or more records.
It can happen i.e. if you do mass Owner Change or import a lot of records using Data Loader.

NEVER use SOQL queries in for loops. It is not allowed and will crash trigger sooner or later as trigger can execute up to 100 SOQL queries.

 

   
© Paweł Woźniak