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
kirankumarreddy punurukirankumarreddy punuru 

Why before and after triggers does not work at the same time?

Hi,
  I have created two triggers on same object account.one trigger is for before update and another is for after update .when i am trying to update the same record it is showing runtime error like this.
Error:Apex trigger AccountTrigger2 caused an unexpected exception, contact your administrator: AccountTrigger2: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.AccountTrigger2: line 6, column 1
 
Can anyone explain the reason and how  to avoid infinite looping .

Thanks,
kiran 
Best Answer chosen by kirankumarreddy punuru
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help u
trigger AccountTrigger2 on Account (before update ) 
{
list <Account> acc = [select Email_Id1__c from Account where ID IN : Trigger.newMap.keySet()];
 for(Account account :acc)
 {
       system.debug('@@email id1:'+ account.Email_Id1__c);
       account.Email_Id1__c ='punurukirankumar92@gmail.com';
       
 }
 }

Please let us know oif this will help u
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Kiran,

You can not update the same record in Aftre trigger. It will give you "Record is read-only" error. Always try to use before trigger to update the same record in trigger.

Please check below blog for more information on trigger framwork
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please check below post:- Salesforce Trigger: When to use Before versus After
http://raydehler.com/cloud/clod/salesforce-trigger-when-to-use-before-versus-after.html

Please let us know if this will help u

Thanks
Amit Chaudhary
kirankumarreddy punurukirankumarreddy punuru
Hi Amit,
           Iam trying to update by creating a instance is it possible?


trigger AccountTrigger2 on Account (after update ) 
{
list <Account> acc = [select Email_Id1__c from Account where ID IN : Trigger.newMap.keySet()];
 for(Account account :acc)
 {
       system.debug('@@email id1:'+ account.Email_Id1__c);
       account.Email_Id1__c ='punurukirankumar92@gmail.com';
       
 }
 }
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help u
trigger AccountTrigger2 on Account (before update ) 
{
list <Account> acc = [select Email_Id1__c from Account where ID IN : Trigger.newMap.keySet()];
 for(Account account :acc)
 {
       system.debug('@@email id1:'+ account.Email_Id1__c);
       account.Email_Id1__c ='punurukirankumar92@gmail.com';
       
 }
 }

Please let us know oif this will help u
 
This was selected as the best answer
kirankumarreddy punurukirankumarreddy punuru
Let me explain what i have done:
Created to two seperate triggers 
1:before update
2:after update
3:Created two custom filelds in account  which is Email_Id__c and Email_Id__c 

// trigger 1

trigger AccountTrigger1 on Account (before update){
    for(Account account : Trigger.New){
        system.debug('@@@@Email Id -:'+account.Email_Id__c);
        if(account.Email_Id__c == null )
        {
         account.Email_Id__c ='kiru2kiran@gmail.com';
         }
    }

// trigger 2
trigger AccountTrigger2 on Account (after update ) 
{
list <Account> acc = [select Email_Id1__c from Account where ID IN : Trigger.newMap.keySet()];
 for(Account account :acc)
 {
       system.debug('@@email id1:'+ account.Email_Id1__c);
       account.Email_Id1__c ='punurukirankumar92@gmail.com';
       }
       


here the first trigger is working fine ,the second trigger is not updating the field?

 
Amit Chaudhary 8Amit Chaudhary 8
Note :- Please never use after trigger to to update the same record. because if you will try the same your trigger will come the recurise.
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html
kirankumarreddy punurukirankumarreddy punuru
Ok thanks Amit, I understand the concept.