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
SureshSuresh 

before and after insert

Hi,

Can any one explain me exactly when to go for before insert and after insert with an example.

Regards,
Suresh.
Best Answer chosen by Suresh
sandeep sankhlasandeep sankhla
HI Suresh,

In Before insert you will not get the record Id and in After Insert you will get the record Id....

If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert..

Example:

If Your Trigger is on Account and you want to update somefields based on phone number, then you can go for before event...

But if you want to insert Contacts based to inserted Account and want to associate them..then in this case you will need Account Id ..so you can ise after insert adn then you can insert contact where you will provide account id..


P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

sandeep sankhlasandeep sankhla
HI Suresh,

In Before insert you will not get the record Id and in After Insert you will get the record Id....

If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert..

Example:

If Your Trigger is on Account and you want to update somefields based on phone number, then you can go for before event...

But if you want to insert Contacts based to inserted Account and want to associate them..then in this case you will need Account Id ..so you can ise after insert adn then you can insert contact where you will provide account id..


P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
king kullaiking kullai
hai suresh this kullayappa,

                              
In Before insert you will not get the record Id and in After Insert you will get the record id

If you are going to do changes from trigger in same object then its good to use Before Event...But if you are going to do changes in another object where you need this object Id and then you should go for After Insert

This is the with in the object by using the before insert.
trigger accountrigger on Account (before delete,before insert,before update) {
    list<opportunity> opptyclosedlos=[select id,name,closedate,stagename from opportunity where 
                                               accountid=:trigger.newmap.keyset() and stagename='closed-lost,closed-won'];
     for(Account a:trigger.new){
         for(opportunity opp:opptyclosedlos){
             if(opp.Accountid==a.id)
             system.debug('>>>>>'+opp);
             
         }
     }                                          
}

when ever the contact(or)  opportunity object feilds are updated by using after insert,


trigger accountTestTrggr  on Account (after insert,after update) {
 List<Account> accountsWithContacts = [select id, name,(select id,firstname, lastname,Description from Contacts) from Account where Id IN :Trigger.newMap.keySet()];         
system.debug('>>>>'+accountsWithContacts );
 List<Contact> contactsToUpdate = new List<Contact>();
     system.debug('>>>'+contactsToUpdate);
    for(Account a: accountsWithContacts){
    for(Contact c: a.Contacts){
    c.Description=c.firstname+c.lastname;
    contactsToUpdate.add(c);
       }
}
update contactsToUpdate;
}