Another Salesforce blog!!!

Salesforce, Apex

triggers: pattern


/*****************************************************************************
 * Trigger     : XXXTrigger
 * Description : XXX__c trigger to handle DML operations through XXXTriggerHelper class
 * 1. After insert;
 * 2. After update;
 * 
 * Project     : XXXXX      
 * Author      : XXXXX
 * Date        : XXXXX
 * Version     : 1.0
 *
 * Version History : 
 * 
 *****************************************************************************/
trigger XXXTrigger on OBJECT_NAME__c (before insert, after insert, before update, after update) 
{
	if(trigger.isBefore){
		if(trigger.isInsert){
		}else if(trigger.isUpdate){
			//when the property is approved, will calculate agents commision.
			XXXTriggerHelper.METHOD_NAME(trigger.new, trigger.oldMap);
		}
	}else if(trigger.isAfter){
		if(trigger.isInsert){
			XXXTriggerHelper.METHOD_NAME(trigger.new, null);
		}else if(trigger.isUpdate){
			XXXTriggerHelper.METHOD_NAME(trigger.new, trigger.oldMap);
			//when the property is working, pending, approved, will calculate the sales number.
			XXXTriggerHelper.METHOD_NAME(trigger.new, trigger.oldMap);
			XXXTriggerHelper.METHOD_NAME(trigger.new, trigger.oldMap);
		}
	}

}

Apex Class:

public static void METHOD_NAME(List<OBJECT_NAME__c> NEWOBJS, Map<Id, OBJECT_NAME__c> oldMAPOBJ)
{
      for(OBJECT_NAME__c newOBJ : NEWOBJS)
      {            
          OBJECT_NAME__c oldBloom = oldMAPOBJ.get(newOBJ.Id);          
          if(newOBJ.Quantity__c != oldBloom.Quantity__c) { // check whether the qty is edited or not  
             //
          }   
      }
}
Trigger NameOfTrigger on CustomSObject__c (after update)
{ 
       for(CustomSObject__c customObject : Trigger.new) { // get old record 
            CustomSObject__c oldCustomObject = Trigger.oldMap.get(customObject.Id);
            // compare old and new values of a particular field 
            if(customObject.fieldName != oldCustomObject.fieldName) 
            { 
                //Trigger e.g. copy the values of a certain field of p and paste them in another sObject 
             } 
        } 
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*