13

According to the documentation, Trigger.old and Trigger.oldMap are only available in update and delete Triggers.

Are these variables null otherwise, or are they just empty? Would Trigger.old.isEmpty() return true if it's referenced in an insert Trigger?

  • easy to test .. – Samuel De Rycke Jun 13 '13 at 15:11
  • 3
    true, but documented no where.. – Matt K Jun 13 '13 at 15:27
10

Trigger.Old is null in a Before Trigger and will therefore throw a NullPointerException for any reference / method invocation on it.

16:08:40.060 (60355000)|USER_DEBUG|[2]|DEBUG|**** Trigger.old null 16:08:40.060 (60362000)|SYSTEM_METHOD_EXIT|[2]|System.debug(ANY) 16:08:40.060 (60503000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

15

Trigger

trigger LogTriggerOld on Account (after insert, after update, after delete, after undelete) 
{
    String BeforeOrAfter = (Trigger.isBefore ? 'Before' : 'After');

    String TriggerType = ''
        + (Trigger.isDelete ? 'Delete' : '')
        + (Trigger.isInsert ? 'Insert' : '')
        + (Trigger.isUpdate ? 'Update' : '')
        + (Trigger.isUnDelete ? 'UnDelete' : '');

    system.debug(BeforeOrAfter + ' ' + TriggerType 
        + ': ' + 'Trigger.old == null    ~ ' + (Trigger.old == null));

    system.debug(BeforeOrAfter + ' ' + TriggerType 
        + ': ' + 'Trigger.oldMap == null ~ ' + (Trigger.old == null));
}

Test

@IsTest
public class TestTriggerOld {
    public static testmethod void InsertUpdateDeleteUndelete() {
        Account a = new Account(Name='Test 1'); insert a;
        a.Name = 'Test 2'; update a;
        delete a;
        undelete a;
    }
}

Log results

USER_DEBUG|[11]|DEBUG|After Insert: Trigger.old == null    ~ true
USER_DEBUG|[14]|DEBUG|After Insert: Trigger.oldMap == null ~ true
USER_DEBUG|[11]|DEBUG|After Update: Trigger.old == null    ~ false
USER_DEBUG|[14]|DEBUG|After Update: Trigger.oldMap == null ~ false
USER_DEBUG|[11]|DEBUG|After Delete: Trigger.old == null    ~ false
USER_DEBUG|[14]|DEBUG|After Delete: Trigger.oldMap == null ~ false
USER_DEBUG|[11]|DEBUG|After UnDelete: Trigger.old == null    ~ true
USER_DEBUG|[14]|DEBUG|After UnDelete: Trigger.oldMap == null ~ true

USER_DEBUG|[11]|DEBUG|Before Insert: Trigger.old == null    ~ true
USER_DEBUG|[14]|DEBUG|Before Insert: Trigger.oldMap == null ~ true
USER_DEBUG|[11]|DEBUG|Before Update: Trigger.old == null    ~ false
USER_DEBUG|[14]|DEBUG|Before Update: Trigger.oldMap == null ~ false
USER_DEBUG|[11]|DEBUG|Before Delete: Trigger.old == null    ~ false
USER_DEBUG|[14]|DEBUG|Before Delete: Trigger.oldMap == null ~ false
0

The above test has a mistake in case it was run as it is written here The second System.debug should print result of Trigger.oldMap, but instead it prints Trigger.old. I am not sure if it will change the result, but for the clearance of test should run it correctly :)

    trigger LogTriggerOld on Account (after insert, after update, after delete, after undelete) 
    {
String BeforeOrAfter = (Trigger.isBefore ? 'Before' : 'After');

String TriggerType = ''
    + (Trigger.isDelete ? 'Delete' : '')
    + (Trigger.isInsert ? 'Insert' : '')
    + (Trigger.isUpdate ? 'Update' : '')
    + (Trigger.isUnDelete ? 'UnDelete' : '');

system.debug(BeforeOrAfter + ' ' + TriggerType 
    + ': ' + 'Trigger.old == null    ~ ' + (Trigger.old == null));

system.debug(BeforeOrAfter + ' ' + TriggerType 
    + ': ' + 'Trigger.oldMap == null ~ ' + (Trigger.oldMap == null));

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.