0

I am reading Trigger Framework and try to follow the architecture in SFDC Trigger Framework .

I notice there no example on comparing with old data.Is this correct if I just do like this in Object_TriggerHandler

  private void setPopulateDivision(){
          for (Opportunity opp : (List<Opportunity>)trigger.new) {
            if((opp.Division__c=='CRM' || opp.Divisions__c=='HR'  && opp.Division__c != Trigger.oldMap.get(opp.Id).Division__c)

                //logic
    }
  }
 }

I notice also there no isBefore and isInsert handler.Anyone got example on how doing validation in trigger framework?

1
  • In the "before insert" situation there is no old record (or ID in the new record); assuming the old value is null in that case is a common approach.
    – Keith C
    Commented Mar 15, 2016 at 8:42

1 Answer 1

1

In a very simple way you can follow this design:

trigger ObjectTrigger on Object__c (after insert, after update, before update)
{
   if(Trigger.isAfter)
   {
       if(Trigger.isUpdate)
       {
           ObjectTriggerHandler.onAfterUpdate(Trigger.new, Trigger.oldMap)
       }
       else if(Trigger.isInsert)
       {
            // Pass only trigger.new or Trigger.newMap in param
            // Trigger.Old or Trigger.OldMap is null here
            ObjectTriggerHandler.onAfterInsert(Trigger.new)
       }
   }
   else if(..)
   {
       if(Trigger....)
       { 
            ....
       }
   }
   ...
}

Trigger Handler

public with sharing class ObjecTriggerHandler
{
    public static void onAfterUpdate(List<Object__c> newList, Map<Id,Object__c> oldMap)
    {
        for(Object__c obj : newList)
        {
           if(Object__c.Field__c != oldMap.get(obj.id).Field__c)
           {
                // Change identified
                // perform Operations 
           }
           else
           {
               // Throw Error
               Object__c.Field__c.addError(' No change is made in the field. Same value: '+obj.Field__c);
           }
       }
    }
}
2
  • How about if we just want to do validation and throw an error if the criteria not fulfill?Is it can be applied in the framework?
    – unidha
    Commented Mar 15, 2016 at 8:56
  • @unidha Throwing error stops the Trigger execution. However, I have updated the design.
    – Ashwani
    Commented Mar 15, 2016 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .