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
MikeGillMikeGill 

Update original object started playing up

Hi All,

 

I am updating the same object using "after update" and it's throwing an error which to me seems impossible as I am using trigger.new and not trigger.old

 

Error:Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.AccountTrigger: line 7, column 3

 

So I decided to perform a simple test in a clean dev org using the following:

 

 

trigger AccountTrigger on Account (after update) {
	
	List<Account> acc=new List<Account>();

	for (Account a: Trigger.new){

		a.Description = 'Test';
		acc.add(a);

 	}

	update acc;


}

 

 

This should work or am I being stupid?

 

Thx

Best Answer chosen by Admin (Salesforce Developers) 
ReidCReidC

It's after update, and then you call update again.

 

Any chance your getting caught in a recursive trigger thing?

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

 

All Answers

ReidCReidC

It's after update, and then you call update again.

 

Any chance your getting caught in a recursive trigger thing?

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

 

This was selected as the best answer
MikeGillMikeGill

I have tried a number of things I know - I believe this would handle recussive

 

 

trigger AccountTrigger on Account (after update) {
	

	List<Account> acc =new List<Account>();

	List<Account> accs = [select Id, Description from Account where Id IN: Trigger.newMap.keySet() limit 1];

	for (Account a: accs){

		a.Description = 'Test';
		acc.add(a);

 	}

	update acc;
	


}

 Any other points - this basic stuff. Having one of those days.

 

I have never updated the sames account before, but the online help suggest it's possible. PLus I have applied the same logic which works for another record type...

 

M

 

MikeGillMikeGill

Before anyone thinks I am mad - I know I could use before update and that would work.

 

What I am actually trying to do is this:

 

Hook into the account update DML when a AccountContactRole is added.

 

I need to record the number of decision makers identified at the Account level.

 

thx

Rajesh ShahRajesh Shah

If you really want to do update to the same record in an after update trigger, you will have to clone the record and do the update on that. By default, you cannot update the same record as in your trigger context. Cloning is an workaround to that. Also, just to be clear, cloning will not create a new record. Below is an example.

 

Account a = new_acc.clone();
update a;

 

Also, this could lead to a infinite loop as suggested earlier. You will have to use Static variables to stop the loop. The following article explains the problem and the solution:

Prevent Infinite for loop