function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
suresh dupadasuresh dupada 

Give me scenario of using trigger.old with sample code

Best Answer chosen by suresh dupada
kaustav goswamikaustav goswami
Trigger.old is handy when you are trying to check certain value in the old context and the new context. Suppose you have a scenario where you need to execute some logic based on specific values of a field. Say Case status. If your requirement is - Execute logic only if case status has been updated from New to In progress.

In this scenario you will have to do something like this in your trigger

trigger caseStatusDemoLogic on Case (before update){
    for(integer i=0; i<Trigger.new.size(); i++){
        if(Trigger.old[i].Status == 'New' && Trigger.new[i].Status == 'In Progress'){
            // do what ever your logic is - like adding the record to a list for further operations
        }
    }
}

It also becomes very useful for before delete scenarios.

If you have to check something before a record is deleted and based on that take some action or decision then you need to refer to the Trigger.old context as in case of delete operation there is no Trigger.New context available.

trigger myAccountTrigger on Account(before delete, before insert, before update,
                                    after delete, after insert, after update) {
if (Trigger.isBefore) {
    if (Trigger.isDelete) {

        // In a before delete trigger, the trigger accesses the records that will be
        // deleted with the Trigger.old list.
        for (Account a : Trigger.old) {
            if (a.name != 'okToDelete') {
                a.addError('You can\'t delete this record!');
            }
        }
    } else {

    // In before insert or before update triggers, the trigger accesses the new records
    // with the Trigger.new list.
        for (Account a : Trigger.new) {
            if (a.name == 'bad') {
                a.name.addError('Bad name');
            }
    }
    if (Trigger.isInsert) {
        for (Account a : Trigger.new) {
            System.assertEquals('xxx', a.accountNumber);
            System.assertEquals('industry', a.industry);
            System.assertEquals(100, a.numberofemployees);
            System.assertEquals(100.0, a.annualrevenue);
            a.accountNumber = 'yyy';
        }

// If the trigger is not a before trigger, it must be an after trigger.
} else {
    if (Trigger.isInsert) {
        List<Contact> contacts = new List<Contact>();
        for (Account a : Trigger.new) {       
            if(a.Name == 'makeContact') {
                contacts.add(new Contact (LastName = a.Name,
                                          AccountId = a.Id));
            }
        }
      insert contacts;
    }
  }
}}}

For further understanding on this context variables please refer to the developer's guide.

http://www.salesforce.com/us/developer/docs/apexcode/

Let us know if you need any more information.

Thanks,
Kaustav

All Answers

kaustav goswamikaustav goswami
Trigger.old is handy when you are trying to check certain value in the old context and the new context. Suppose you have a scenario where you need to execute some logic based on specific values of a field. Say Case status. If your requirement is - Execute logic only if case status has been updated from New to In progress.

In this scenario you will have to do something like this in your trigger

trigger caseStatusDemoLogic on Case (before update){
    for(integer i=0; i<Trigger.new.size(); i++){
        if(Trigger.old[i].Status == 'New' && Trigger.new[i].Status == 'In Progress'){
            // do what ever your logic is - like adding the record to a list for further operations
        }
    }
}

It also becomes very useful for before delete scenarios.

If you have to check something before a record is deleted and based on that take some action or decision then you need to refer to the Trigger.old context as in case of delete operation there is no Trigger.New context available.

trigger myAccountTrigger on Account(before delete, before insert, before update,
                                    after delete, after insert, after update) {
if (Trigger.isBefore) {
    if (Trigger.isDelete) {

        // In a before delete trigger, the trigger accesses the records that will be
        // deleted with the Trigger.old list.
        for (Account a : Trigger.old) {
            if (a.name != 'okToDelete') {
                a.addError('You can\'t delete this record!');
            }
        }
    } else {

    // In before insert or before update triggers, the trigger accesses the new records
    // with the Trigger.new list.
        for (Account a : Trigger.new) {
            if (a.name == 'bad') {
                a.name.addError('Bad name');
            }
    }
    if (Trigger.isInsert) {
        for (Account a : Trigger.new) {
            System.assertEquals('xxx', a.accountNumber);
            System.assertEquals('industry', a.industry);
            System.assertEquals(100, a.numberofemployees);
            System.assertEquals(100.0, a.annualrevenue);
            a.accountNumber = 'yyy';
        }

// If the trigger is not a before trigger, it must be an after trigger.
} else {
    if (Trigger.isInsert) {
        List<Contact> contacts = new List<Contact>();
        for (Account a : Trigger.new) {       
            if(a.Name == 'makeContact') {
                contacts.add(new Contact (LastName = a.Name,
                                          AccountId = a.Id));
            }
        }
      insert contacts;
    }
  }
}}}

For further understanding on this context variables please refer to the developer's guide.

http://www.salesforce.com/us/developer/docs/apexcode/

Let us know if you need any more information.

Thanks,
Kaustav
This was selected as the best answer
Ravikant kediaRavikant kedia

You can get Trigger.old in update and delete case.

public class AccountTriggerHelper {
   public static void createCaseWhenNeeded(List<Account> accounts, Map<Id, Account> oldMap){
        List<Case> casesToCreate = new List<Case>();
        for(Account acc:accounts){
            Account beforeUpdate = oldMap.get(acc.Id);
            if( beforeUpdate.LastName != 'CreateCase' && acc.LastName == 'CreateCase' ) {
                Case caseToAdd = new Case();
                caseToAdd.AccountId = acc.Id;
                caseToAdd.RecordTypeId = '012600000005DYN';
                caseToAdd.Origin = 'Receptionist';
                caseToAdd.Products__c = 'Other';
                casesToCreate.add(caseToAdd);
            }
        }
        insert casesToCreate;
   }
}

This would allow you to just call the following in your Account trigger:

AccountTriggerHelper.createCaseWhenNeeded(Trigger.new, Trigger.oldMap);
Arpita NayakArpita Nayak
Can ane one give some other trigger.old examples for more clarity.