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
Ashritha ReddyAshritha Reddy 

when i have use trigger.new and trigger.old please can any one explain indetailed with small scenarios please?

when i have use trigger.new and trigger.old please can any one explain indetailed with small scenarios please?
sandeep@Salesforcesandeep@Salesforce
Hi Ashritha, 

This very simple concept when to use new or when after. 

For insert case as you know in insert we do not have any information of older state ( because data is inserting now) so Trigger.old is not possible only Trigger.new is possible.

For Update case as record is already exist and we are making some changes so we get old and new instance thats why in update case Trigger.New and Trigger.Old are possible. 

In Delete case, after deletion there is no existance of record so we can not get new instance thats why only Trigger.old is possible for delete case. 

Please mark this asnwer as best anwer so that it would be helpful for others. 

Thanks 
Sandeep Singhal
http://www.codespokes.com/
CloudGeekCloudGeek
Hello,

I would suggest you to read this post :
https://developer.salesforce.com/forums/?id=906F0000000BQmoIAG


Note: Mark this as Solution if it helps!

Cheers,
KVin
Rohit K SethiRohit K Sethi
Hi ,

Basically trigger.new and trigger.old are depend on Old or New values of Object. You can better understand by below description.

Old :
Returns a list of the old versions of the sObject records.
Note that this sObject list is only available in update and delete triggers.

New :
Returns a list of the new versions of the sObject records.
Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.


Thanks.
Amit Chaudhary 8Amit Chaudhary 8
1) isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or anexecuteanonymous() API call.
2) isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or theAPI.
3) isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or theAPI.
4) isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or theAPI.
5) isBefore Returns true if this trigger was fired before any record was saved.
6) isAfter Returns true if this trigger was fired after all records were saved.
7) isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
8) new Returns a list of the new versions of the sObject records.Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
9) newMap A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.
10) old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
11) oldMap A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.
12) size The total number of records in a trigger invocation, both old and new.

Please check Trigger Context Variables for same
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm

If you want to learn Trigger i will commend your please use trailhead
1) https://developer.salesforce.com/trailhead/module/apex_triggers

Let us know if this will help you

Thanks
Amit Chaudhary