Using Trigger.old & Trigger.new to detect when field values change

On a recent post in the Apex Development forums a user was trying to have an Account trigger automatically create a Case record when certain IF criteria were met. His main dilemma was trying to figure out how to ensure the case is only created once while still allowing other fields on the account record (Name, Phone Number, etc.,) to be edited. He also wanted the trigger to re-fire if any of the fields in the IF criteria changed in value. Other developers suggested creating a field, IsCaseCreatedc, check box to store the value of whether or not a case had been created.  Then wrap his current logic in an IF clause that verifies IsCaseCreatedc is set to FALSE before creating a new case. While this will ensure that the case is only created once, he would still need an additional workflow/trigger to reset the IsCaseCreated__c field if any of the chosen field values change. This approach works just fine, I just prefer not to create fields if unnecessary.

My solution to his problem was to create an IF clause that compared the values of Trigger.old to the values of Trigger.new to see if any of the field values changed. If a change was detected, then re-fire his original logic. Here is his original code:

trigger CreateVenereRequestIdCase on Account (before update) {

  Case[] newCase = new Case[0];
  for (Account a : Trigger.new) {
               Account beforeUpdate = System.Trigger.oldMap.get(a.Id);
                 if((a.RecordTypeId == '012700000009Tor' || a.RecordTypeId == '012700000009JNI')
                 && a.Venere_Module_ID__c == null && a.Venere_ID__c == null && a.Request_Venere_ID__c == TRUE && beforeUpdate.Request_Venere_ID__c == FALSE)
                   system.debug('Here is the account id: ' + a.Id);
                        newCase.add(new Case(
                          accountID = a.id,
                          Origin = 'Market Manager',
                          Priority = 'Medium',
                          RecordTypeID = '012T00000000Obu', //HDM - Venere Request ID
                          Status = 'New',
                          Subject = 'Venere ID Request',
                          Description = 'Please create a new Venere ID for ' + a.Name)
                                                                );                              

                }
                insert newCase;

}

My approach essentially wraps his current logic in an IF clause that compares field values of Trigger.old to Trigger.new. The modified code would look something like this.

trigger CreateVenereRequestIdCase on Account (before update)
{
    Case[] newCase = new Case[0];
    for (Account a : Trigger.new)
    {
        Account beforeUpdate = System.Trigger.oldMap.get(a.Id); 

        if(beforeUpdate.RecordTypeId != a.RecordTypeId
            || beforeUpdate.Venere_Module_ID__c != a.Venere_Module_ID__c
            || beforeUpdate.Venere_ID__c != a.Venere_ID__c
            || beforeUpdate.Request_Venere_ID__c != a.Request_Venere_ID__c)
        {
                if((a.RecordTypeId == '012700000009Tor' || a.RecordTypeId == '012700000009JNI')
                    && a.Venere_Module_ID__c == null
                    && a.Venere_ID__c == null
                    && a.Request_Venere_ID__c == TRUE
                    && beforeUpdate.Request_Venere_ID__c == FALSE)
                {
                    system.debug('Here is the account id: ' + a.Id);
                    newCase.add(new Case(
                        accountID = a.id,
                        Origin = 'Market Manager',
                        Priority = 'Medium',
                        RecordTypeID = '012T00000000Obu', //HDM - Venere Request ID
                        Status = 'New',
                        Subject = 'Venere ID Request',
                        Description = 'Please create a new Venere ID for ' + a.Name)
                    );
                }
        }
    }

    insert newCase;
}

Now his trigger will create a new case every time one of the chosen fields change and no extra fields needed to be created in the process.

Vote on HN


comments powered by Disqus