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
M SreekanthM Sreekanth 

way it is working both after insert and before insert,can any one elaborate this

My scenario was whenever account record is created then automatically i want to create contact records.it's an interview question i got from yesterday i writtened the logic inside if(Trigger.isAfter&&isInsert)

Trigger:-
-----------

trigger AccountTriggerEx on Account (before insert,after insert) {
/*way it is working both after insert and before insert */
    if(Trigger.isBefore && Trigger.isInsert){
list<contact> conList = new list<contact>();
    for(account a:Trigger.new){
        contact c = new contact();
        c.LastName=a.Name;
        c.AccountId=a.Id;
        c.Phone=a.Phone;
        conList.add(c);
    }
    insert conList;
    }
      if(Trigger.isAfter && Trigger.isInsert){
list<contact> conList = new list<contact>();
    for(account a:Trigger.new){
        contact c = new contact();
        c.LastName=a.Name;
        c.AccountId=a.Id;
        c.Phone=a.Phone;
        conList.add(c);
    }
    insert conList;
    }
}
 
Best Answer chosen by M Sreekanth
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The main issue with your code is you have written code for before insert and after insert context.

Ideally this should be after insert context as we are creating other record related to Account. Also Account Id wont be available during before insert so this is the main error.

The trigger shoud be only on After insert. 

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 effect changes in other records.
 

Let me know if you face any issues.

 

If this solution helps, Please mark it as best answer.

 

Thanks,

All Answers

M SreekanthM Sreekanth
They said my logic was wrong
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The main issue with your code is you have written code for before insert and after insert context.

Ideally this should be after insert context as we are creating other record related to Account. Also Account Id wont be available during before insert so this is the main error.

The trigger shoud be only on After insert. 

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 effect changes in other records.
 

Let me know if you face any issues.

 

If this solution helps, Please mark it as best answer.

 

Thanks,

This was selected as the best answer
M SreekanthM Sreekanth
Hi Praveen,
   i am not gettiong any error,yesterday i attended one interveiw i written the below code They said it was wrong what is wrong i am not getting,because both (before and after insert)working fine from above code
trigger AccountTriggerEx on Account (after insert) {
list<contact> conList = new list<contact>();
    for(account a:Trigger.new){
        contact c = new contact();
        c.LastName=a.Name;
        c.AccountId=a.Id;
        c.Phone=a.Phone;
        conList.add(c);
    }
    insert conList;
    }
}
ThankYou For Replay
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sreekanth,

Yes there wont be any error if you use both before and after insert but the implementation is wrong.

The one which you wrote above is correct and has no issues in it.

Thanks,