Ask Search:
Swamy Viswanadh MaddimsettySwamy Viswanadh Maddimsetty 

Triggers related question

What is syntactial difference between before triggers and after triggers, is it only differentiated by events like before insert / before update / before detete are called before triggers and same like for after triggers.

This is my reference information from salesforce Developers guide.

Before triggers are used to update or validate record values before they’re saved to the database.
After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.
Best Answer chosen by Swamy Viswanadh Maddimsetty
Meghna VijayMeghna Vijay
@Swamy
Trigger.old will have all the 10 records before updation --> Correct
Trigger.new will it contain any records? --> Yes it will, it will contain a list of those records in which you have made any update. Basically in after insert/update the record has been saved to the database and in before insert/update record is committed but it doesn't mean that Trigger.new will be null in before event. 

Let me know if you need more clarification.

Happy Coding !

 

All Answers

Meghna VijayMeghna Vijay
Hi Swamy,

If you want to make changes in the same object on which you are creating trigger then you can do that using before insert/update. In before event record is committed but not saved to the database and that's why it is not in read-only mode.

If you want to make changes in another object directly/indirectly related to it then you can do that using after insert/update.

For Example:-
Case (C1 )--> update Status to Progress.
You'll be using before insert/update
Case(C1) --> Account (A1) --> Update Account's Field let's say YYY value to ZZZ
You'll be using after insert/update.

Let me know if you need further clarification.
Thanks
Swamy Viswanadh MaddimsettySwamy Viswanadh Maddimsetty
Thanks Meghna for the explanation.

To confirm my understanding, Suppose an Account Object having 10 Records in Database (which were committed and saved).
Now I am going to update all the 10 Records , we have a trigger (say) with Before insert / Before update ,
My understanding is that
Trigger.old will have all the 10 records before updation. / Trigger.new will it contain any records my understanding is zero because we are using before insert / before update.

Alternate scenario is if we are using after insert / after update
Trigger.old will still have 10 recordss before updation and Trigger.new will contain latest 10 records after updation.

I hope this is NOT so confusing. Thanks for your reply though.
Meghna VijayMeghna Vijay
@Swamy
Trigger.old will have all the 10 records before updation --> Correct
Trigger.new will it contain any records? --> Yes it will, it will contain a list of those records in which you have made any update. Basically in after insert/update the record has been saved to the database and in before insert/update record is committed but it doesn't mean that Trigger.new will be null in before event. 

Let me know if you need more clarification.

Happy Coding !

 
This was selected as the best answer
Rajasekhar TandasaRajasekhar Tandasa
Hi Meghana
Trigger.old contains old version of the records right?