Difference Between Trigger.new And Trigger.old In Apex?

devquora
devquora

Posted On: Feb 22, 2018

 

    1 Answer Written

  •  devquora
    Answered by Satyam

    Trigger.new: Tigger.new, simply put is a command that returns the entire list of new records that are trying to insert in the database or have recently been added in it. This means that the list which will come up includes those records which are not yet saved in the database. This functioning is available after insert, after update triggers, before insert, before update triggers and deleted triggers.

    The list of all the records can easily be modified in before triggers and only in before triggers(one should keep this thing in mind). It also returns the list of latest versions of the sObject record and this list is available only in insert or update triggers. Later the records can be modified before triggers. Through this, you can get the currently modified records which also have field values for the update or insertion whichever is required. For the custom object, it refers to the command as- List<Custom_obj__c>

    Through trigger.new you can use an object to change the field of it. But remember only in before triggers. The problem with the after triggers is that trigger.new is not saved in them.

    Trigger.old: it returns the value of records that are updated along with the new rules. The list of records already exists in the database where Trigger.old is available before the update, before delete, after an update or after deleting triggers. It returns the list of all the old versions of sObject records. The important point to note is that sObject list is only available in the delete triggers or update triggers.

    With this, you can have the field values which are older than the latest ones and it will be updated in the recent transaction along with the values that are present in the Trigger.new. Also, this is not available during the insert. While writing the codes it is important to consider that Trigger.old Map is only valid for the update or deletes events and unlike Trigger.new it cannot be used for insert event triggers.

Related Questions

Please Login or Register to leave a response.

Related Questions

Apex Interview Questions

Tell me something about Abstract Class?

 The abstract classes in Salesforce Apex are the classes that involve at least one dynamic strategy. A conjectural meth..

Apex Interview Questions

Clarify a couple of considerations required for the Static Keyword in Apex.

 Here are a couple of considerations required for the Static keyword in Apex.  You cannot make an Apex class static..

Apex Interview Questions

Can you state some o the uses of APEX?

fabricates proficient looking web applications that are both quick and secure. Keeps running on and lives in the Oracle..