You need to sign in to do that
Don't have an account?

Trigger to count the number of record updation in a record of Account
Please help me in this: I have created a counter field in account and have written the code like this
trigger AccountUpdationCounter on Account (after update) {
for(Account acts:trigger.new){
if(Trigger.isUpdate){
acts.Counter__c=0;
acts.Counter__c=acts.Counter__c+1;
}
update acts;
}
}
Getting below error after updating from UI:Error:Apex trigger AccountUpdationCounter caused an unexpected exception, contact your administrator: AccountUpdationCounter: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.AccountUpdationCounter: line 4, column 1
trigger AccountUpdationCounter on Account (after update) {
for(Account acts:trigger.new){
if(Trigger.isUpdate){
acts.Counter__c=0;
acts.Counter__c=acts.Counter__c+1;
}
update acts;
}
}
Getting below error after updating from UI:Error:Apex trigger AccountUpdationCounter caused an unexpected exception, contact your administrator: AccountUpdationCounter: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.AccountUpdationCounter: line 4, column 1
All Answers
Could you please explain me why you have taken before update?
Thanks,
Silpi
You can not perform dml operations in an after insert/after update trigger on same object on which trigger is developed.
Because in an after insert/update trigger, the records are read only in that context as they have been written, but not committed, to the database.
You can update/assign values in the trigger context in before trigger.
Regards,
Waqar