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
jaanvivekjaanvivek 

Trigger's basic Information.

Hello All,

 

I am new to SFDC tech. I do not work on this tech but i learnt it through docs and blogs.

 

I have gone through  all satndard documents but I did not get practical knowledge regarding my queries for Triggers.

 

I  have following queries regarding Triggers-

 

1- In real time what is "before" and  "after" trigger.

 

2- In which scenarios we should go for "before" and "after".

 

3-In reference of salesforce.com database what is significance of "before" and "after".

 

4- practical difference between "Trigger.new" and "Trigger.old"

 

5- Are there any best practices where we do use "Trigger.new" and "Trigger.old".

 

I would like to say thanks in advance for your valuable suggestions.

 

I learnt a lot from this community.

 

I would appreciate if anyone please provide some real time(practical) explanation. It will help me to digest it.

 

 

Thanks again for your time and knowledge sharing.

 

Thanks,

jaanVivek

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi,

 

Everyone here is passionate about SFDC! So, we're all glad to help you learn more and more of it while we ourselves keep learning :)

 

 

Regarding your queries, I will share my knowledge on the same for each point.

 

1- In real time what is "before" and  "after" trigger.


Suppose you are creating a new Account record from the UI, now when you click on save, a user would only see the record getting saved once he moves to the detail page.

However, when you click save, record does not directly go into your database. A before insert trigger would get fired, it will execute the code inside it and after the execution is completed, only then the record gets saved. Now, once your record is in your database, it's time for the after insert trigger to fire.

 

So, as the names suggest, a before trigger would fire before your data is committed in the database and same goes for after, it fires only after your data gets committed in the database.

 

2- In which scenarios we should go for "before" and "after".

 

The ideal scenario for any "BEFORE" trigger would be when inside your trigger, you want to modify the same record.

Example: You are inserting an Account, now you want to update a field on the same Account based on some logic. Here you write your login inside a "BEFORE" trigger, because you don't want to commit to the database unnecessarily by first inserting the record and then again updating it.

 

However, there may be a case when you're inserting an Account and you want a related Contact record to be created based on some logic automatically. So in this case, you need the Account Id for creating the Contact - Account relationship. Id is generated for a record only after it enters your database. So you use after triggers for such scenarios.

 

3-In reference of salesforce.com database what is significance of "before" and "after".

 

With reference to the sf database, as I wrote above, it is just the order in which your execute your logic when a record is created/updated or deleted.

 

Example : for any record getting updated, this will be the flow (only from the trigger perspective) :

before update - record is updated - after update

 

Here, between the "before update" and "record is updated", there may be a validation logic in your trigger which may even avoid the record from getting committed to the database.

 

Best example for this is a duplication catcher trigger, you don't want duplicate records based on names on Contacts. you write a trigger on Contact (before insert and before update), you check for the existing records if any of it matches the current record, if it does, you DO NOT save the record, instead you throw an error message for the same.

 

4- practical difference between "Trigger.new" and "Trigger.old"


Both are SObject lists of the record on which the trigger is written. Trigger.new will have current details of the record getting inserted or updated, whereas trigger.old will have the existing details for a record before it is updated or deleted.

 

Practically, take this example:

 

You are creating a new Account, in this case since it is a new record, no details exist in the database, so there is no trigger.old for insert cases.
 

Trigger.new here will have all the Account details that you have entered while saving the record, except the Id (REMEMBER : Id is generated only AFTER record enters your database).

 

However, while updating a record, the record is already in your database. So if I'm changing the Account name from "Vishal" to "Vishal N", trigger.old will show me "Vishal" (existing value) and trigger.new will show me "Vishal N" (new value).

 

5- Are there any best practices where we do use "Trigger.new" and "Trigger.old".


Best practices would only include the knowledge of data that these lists have. Once you know, what data each one of them has, you will exactly know the one to be used at a point of time.

 

Still, as I said earlier, trigger.new will have the new data (for insert and update), while trigger.old will have the existing data (update and delete).

 

 

Let me know if you need to know anything else or if you got any doubts in any of the points above. Triggers are something you'll learn more and more about once you start writing them.

 

You can start with simple triggers like : updating a field on the same object on insert or creating a related Contact everytime an Account is created based on some conditions.

 

And then move onto some complex tasks, you'll find loads of them here itself!

 

Good luck and happy learning :)

All Answers

vishal@forcevishal@force

Hi,

 

Everyone here is passionate about SFDC! So, we're all glad to help you learn more and more of it while we ourselves keep learning :)

 

 

Regarding your queries, I will share my knowledge on the same for each point.

 

1- In real time what is "before" and  "after" trigger.


Suppose you are creating a new Account record from the UI, now when you click on save, a user would only see the record getting saved once he moves to the detail page.

However, when you click save, record does not directly go into your database. A before insert trigger would get fired, it will execute the code inside it and after the execution is completed, only then the record gets saved. Now, once your record is in your database, it's time for the after insert trigger to fire.

 

So, as the names suggest, a before trigger would fire before your data is committed in the database and same goes for after, it fires only after your data gets committed in the database.

 

2- In which scenarios we should go for "before" and "after".

 

The ideal scenario for any "BEFORE" trigger would be when inside your trigger, you want to modify the same record.

Example: You are inserting an Account, now you want to update a field on the same Account based on some logic. Here you write your login inside a "BEFORE" trigger, because you don't want to commit to the database unnecessarily by first inserting the record and then again updating it.

 

However, there may be a case when you're inserting an Account and you want a related Contact record to be created based on some logic automatically. So in this case, you need the Account Id for creating the Contact - Account relationship. Id is generated for a record only after it enters your database. So you use after triggers for such scenarios.

 

3-In reference of salesforce.com database what is significance of "before" and "after".

 

With reference to the sf database, as I wrote above, it is just the order in which your execute your logic when a record is created/updated or deleted.

 

Example : for any record getting updated, this will be the flow (only from the trigger perspective) :

before update - record is updated - after update

 

Here, between the "before update" and "record is updated", there may be a validation logic in your trigger which may even avoid the record from getting committed to the database.

 

Best example for this is a duplication catcher trigger, you don't want duplicate records based on names on Contacts. you write a trigger on Contact (before insert and before update), you check for the existing records if any of it matches the current record, if it does, you DO NOT save the record, instead you throw an error message for the same.

 

4- practical difference between "Trigger.new" and "Trigger.old"


Both are SObject lists of the record on which the trigger is written. Trigger.new will have current details of the record getting inserted or updated, whereas trigger.old will have the existing details for a record before it is updated or deleted.

 

Practically, take this example:

 

You are creating a new Account, in this case since it is a new record, no details exist in the database, so there is no trigger.old for insert cases.
 

Trigger.new here will have all the Account details that you have entered while saving the record, except the Id (REMEMBER : Id is generated only AFTER record enters your database).

 

However, while updating a record, the record is already in your database. So if I'm changing the Account name from "Vishal" to "Vishal N", trigger.old will show me "Vishal" (existing value) and trigger.new will show me "Vishal N" (new value).

 

5- Are there any best practices where we do use "Trigger.new" and "Trigger.old".


Best practices would only include the knowledge of data that these lists have. Once you know, what data each one of them has, you will exactly know the one to be used at a point of time.

 

Still, as I said earlier, trigger.new will have the new data (for insert and update), while trigger.old will have the existing data (update and delete).

 

 

Let me know if you need to know anything else or if you got any doubts in any of the points above. Triggers are something you'll learn more and more about once you start writing them.

 

You can start with simple triggers like : updating a field on the same object on insert or creating a related Contact everytime an Account is created based on some conditions.

 

And then move onto some complex tasks, you'll find loads of them here itself!

 

Good luck and happy learning :)

This was selected as the best answer
jaanvivekjaanvivek

Thank you so much Vishal for your valuable and helpful suggestion.

 

Thanks,

JaanVivek

jaanvivekjaanvivek

I have one Doubt about below mentioned code for trigger.

trigger calculateTotal on SuperMarket__c (before insert, before update)
{
   if(trigger.isbefore)
   {
       if(trigger.isinsert)
       {
         for(SuperMarket__c custSup:Trigger.new)
         {
             if(custSup.Type__c=='hot')
             {
                custSup.Total__c=100;  
                system.debug('id is========>' +custSup.Id);
                system.debug('Name of account before insert is======>' +custSup.Name);
             
             }
                 
         }  
       
       }
              
     }
       
       if(trigger.isbefore)
       {
          if(trigger.isupdate)
          {
              for(SuperMarket__c custSup:Trigger.new)
              {
                 
                
                 custSup.Total__c=2000;
                 system.debug('id is========>' +custSup.Id);
                 system.debug('Name of account before insert is======>' +custSup.Name);
                 
                              
                 
              
              }
          
          }
       
       
       
       }
   
      
   
}

 IN this code i am inserting one record in SuperMarket__c and if it's Type__c=='hot'. It's setting Total=100.

 

 after inserting record I did not get Id and  Name.

 like-

system.debug('id is========>' +custSup.Id);
system.debug('Name of account before insert is======>' +custSup.Name);

 


But when I am updating(before) the same record I am getting Ids and Name also.

 

Both are before so why we are getting Ids and Name in "before update" but not in "before Insert".

 

Could you please help to get this.

 

Thanks,

JannVivek

 

vishal@forcevishal@force

Hi, As I told you earlier, ID is generated after a record is saved in the database. Before Insert will fire before the record is actually saved, so you won't get the ID as it still doesn't exist when the trigger is executed.

 

Name field you should get even in before inserts. Is it an auto-number field?

jaanvivekjaanvivek

Thanks Vishal. I am ok with "before insert" but

if(trigger.isbefore)
       {
          if(trigger.isupdate)
          {
              for(SuperMarket__c custSup:Trigger.new)
              {
                 
                
                 custSup.Total__c=2000;
                 system.debug('id is========>' +custSup.Id);
                 system.debug('Name of account before insert is======>' +custSup.Name);
                 
                              
                 
              
              }
          
          }
       
       
       
       }

 In "before update" I am getting IDs and Name also.

 

So is it saved and commited in DB after execution of "before update".

 

could you please clarify it.

 

Thanks,

JaanVivek

vishal@forcevishal@force

Hi,

 

Yes when you say UPDATE, you are modifying a record which is already existing. So you will get the ID here because the record already exists, you are just updating it.

prashanth murukondaprashanth murukonda
Can anyone please explain what are Extensions in Salesforce.And What does Extensions do?