Trigger.new : Returns a list of the new versions
of the sObject records. Note that this sObject list is only
available in insert and updatetriggers, and the records can only
be modified in before triggers.
Trigger.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.
trigger.new is holding your object record data which is currently set by user and trigger.old contains history object records. like if we have Opportunity trigger. and in this Opportunity.trigger we have even after update and if you need to compute if there is any change in Opportunity stage then you can figure out this changes using trigger.new and trigger.old values.
Trigger.new : Returns a list of the new versions
of the sObject records. Note that this sObject list is only
available in insert and updatetriggers, and the records can only
be modified in before triggers.
Trigger.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.
Suppose i am writing a trigger on Account object and the trigger fires before/after a single/many record(s) is/are updated. Now trigger.new states Returns a list of the new versions of the sObject records. Can someone please explainn what trigger.new will contain? Can i see this list using system.debug in developer console?
Better yet, set a heap dump to see the list of values in a tree.
In the developer console, create this trigger.
trigger MyTrigger on Account (before update) {
List<Account> newTriggers = Trigger.new;
List<Account> oldTriggers = Trigger.old;
System.debug('We want to set the heap dump marker here');
}
On line 4 (the debug statement) click on the gutter to create a heap dump location. You will see it listed in the Heap Dump tab.
Then execute anonymous the following.
Account a = [Select Name from account limit 1];
a.name = 'Test Name';
update a;
When that is done executing, you will see a new heap dump appear (like a log) in the Heap Dump tab. Open it, then on the Symbols tab, double click on "this", then double click on "newTrigger" then double click on "0". You will see Name is set to "Test Name". Do the same for "oldTrigger" and you will see the previous value. For me it was just "test".
Thanks a lot Thomas for a quick response..Its clear to me now....i did it with 2 rows and its fine...i will do the same for oldmap and newmap as well...i guess it would be messy when i do this on bulk triggers [?]
I'm not sure what you mean by "i will do the same for oldmap and newmap". You typically only need to modify the new map. For example, lets say you want to prepend a value to the account name. You would do something like the following.
trigger MyTrigger on Account (before update) {
for (Account account : Trigger.new) {
account.name = 'PRE-'+account.name;
}
}
You would use the old map is to see when a field has changed, or to compute some value using the two, for example:
trigger MyAccount2 on Account (before update) {
for (Account account : Trigger.new) {
Account oldAccount = Trigger.oldMap.get(account.ID);
if (account.Name.equals(oldAccount.Name)) {
// Fields was updated - Do what you need to do
// For example, compute a new field
// account.NameSizeDelta__c = oldAccount.name.length() - account.name.length();
}
}
}
Trigger.new - It returns the list of new version of sobject records. This is only available for insert and update trigger. Trigger.old- It returns the list of old version of sobject records. This is only avaiable in update and delete trigger.
For example you want to add value to the account record. Here you will use trigger.new
Trigger.old you can use to see the value when the field is changed.
Hi Pacts, Trigger.new and Trigger.old both are context variables in Salesforce.
Trigger.New: Trigger.new returns List of new records which are trying to insert into Database. This is available in Before Insert, Before Update, After Insert, After Update Triggers and undelete Triggers. This list of records can only modified in Before triggers.
Trigger.Old: Trigger.old returns List of old records which are updated with new values. These List of records already there in Database. Trigger.old available in Before update, after update, Before Delete and After Delete triggers.
Example:I have a scenario like when Account Phone number is updated then Account Description value should be updated with old phone number+new phone number.
trigger NewVsOld on Account (before update) { for(integer i=0; i<trigger.new.size();i++){ /*if Old Phone number andNew phone number are not same then Description field will be updated with Old phone number+New phone number. if not same no change will be in Description field*/ if(trigger.old[i].phone!=trigger.new[i].phone){ trigger.new[i].description='old phone number is ' + trigger.old[i].phone + ' and New Phone number is ' +trigger.new[i].phone ; } } }
Trigger.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.
Trigger.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.
For more info go through this link:
http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
All Answers
Hi,
The Following Link provide u solution needed
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
Trigger.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.
Trigger.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.
For more info go through this link:
http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
Suppose i am writing a trigger on Account object and the trigger fires before/after a single/many record(s) is/are updated. Now trigger.new states Returns a list of the new versions of the sObject records. Can someone please explainn what trigger.new will contain? Can i see this list using system.debug in developer console?
Many Thanks in advance.
Better yet, set a heap dump to see the list of values in a tree.
In the developer console, create this trigger.
On line 4 (the debug statement) click on the gutter to create a heap dump location. You will see it listed in the Heap Dump tab.
Then execute anonymous the following.
When that is done executing, you will see a new heap dump appear (like a log) in the Heap Dump tab. Open it, then on the Symbols tab, double click on "this", then double click on "newTrigger" then double click on "0". You will see Name is set to "Test Name". Do the same for "oldTrigger" and you will see the previous value. For me it was just "test".
Hope that helps.
I'm not sure what you mean by "i will do the same for oldmap and newmap". You typically only need to modify the new map. For example, lets say you want to prepend a value to the account name. You would do something like the following.
You would use the old map is to see when a field has changed, or to compute some value using the two, for example:
Thanks yar.. Thanks for your information.
https://www.sfdc-lightning.com/2018/09/11triggers-in-salesforce-examples.html
Trigger.old- It returns the list of old version of sobject records. This is only avaiable in update and delete trigger.
For example you want to add value to the account record. Here you will use trigger.new
Trigger.old you can use to see the value when the field is changed.
Pacts,
Trigger.new and Trigger.old both are context variables in Salesforce.
Trigger.New: Trigger.new returns List of new records which are trying to insert into Database. This is available in Before Insert, Before Update, After Insert, After Update Triggers and undelete Triggers. This list of records can only modified in Before triggers.
Trigger.Old: Trigger.old returns List of old records which are updated with new values. These List of records already there in Database. Trigger.old available in Before update, after update, Before Delete and After Delete triggers.
Example: I have a scenario like when Account Phone number is updated then Account Description value should be updated with old phone number+new phone number.
trigger NewVsOld on Account (before update) {
for(integer i=0; i<trigger.new.size();i++){
/*if Old Phone number andNew phone number are not same then Description field will be updated with Old phone number+New phone number. if not same no change will be in Description field*/
if(trigger.old[i].phone!=trigger.new[i].phone){
trigger.new[i].description='old phone number is ' + trigger.old[i].phone + ' and New Phone number is ' +trigger.new[i].phone ;
}
}
}
Thank you,
Venu.