You need to sign in to do that
Don't have an account?
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
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
Please let us know oif this will help u
All Answers
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
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';
}
}
Please let us know oif this will help u
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?
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html