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
jyotijyoti 

Apex trigger yields "Record is read-only"

The following is a trigger that saves correctly to Sandbox.  In it, I want to set the PP_CLIENT_DIR_VP__c to a specific value after an update or creation of an Account record.
 

Trigger UpdateTMID on Account (after insert, after update)

{

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

for (Account acct:Trigger.new)

{

Integer i = 1;

Account a = new Account(Id = acct.Id);

system.debug('This is the number of times it is has gone through the loop' +i);

acct.PP_CLIENT_DIR_VP__c = 'JC13';

acctList.add(acct);

i++;

}

// update the Accounts

update acctList;

}

 

When I attempt to update an Account, however, I receive an error System.Exception: Record is read-only.  The curious thing is 1) I own the Account record and 2) I have written a similar trigger on Contract without this issue.

Additionally, I am setting Account a = new Account(Id = acct.Id); even though I don't intend to create a new Account but rather update the Account that has triggered.  Is this correct?  Incidentally, when I comment out this line, I either get the read-only error or the for loops more than once for one record update.

Any thoughts?

TehNrdTehNrd
Trigger needs to execute before insert and before update.

Also you don't need to update the accounts at the end of the trigger you only need to do this when you want to modify records that are not being processed by then trigger.

Try this:
Code:
Trigger UpdateTMID on Account (before insert, before update){

 for (Account acct:Trigger.new){

  Integer i = 1;

  system.debug('This is the number of times it is has gone through the loop' +i);

  acct.PP_CLIENT_DIR_VP__c = 'JC13';

  i++;
 }
}

 



Message Edited by TehNrd on 01-10-2008 01:33 PM
jyotijyoti

Thanks that worked although I would've expected it to work on after update/insert.

Also, if I'm loading several thousand account records I'll need the List and to modify the records in bulk?

TehNrdTehNrd
You can not make changes after an update because the record has already been updated.

The snippet about will be able to process as many records as you can throw at it.


Message Edited by TehNrd on 01-11-2008 08:52 AM
Akshay MunjalAkshay Munjal
If we create a new instance of an SObject in the Apex Trigger in memory using the Id of the newly created record as provided in the After Trigger context, we can perform an Update DML statement and not get a read only error. This is because in Apex, the SObject is seen as a new reference (even though the records have the same SFDC ID) and therefore is eligible for DML operations.
Ramnath kumar 9Ramnath kumar 9
Trigger UpdateTMID on Account (after insert)
{

List<Account> acctList = new List<Account>();
Integer i = 1;
for (Account acct:Trigger.new)
{
Account a = new Account(Id = acct.Id);
system.debug('This is the number of times it is has gone through the loop' +i);
a.PP_CLIENT_DIR_VP__c = 'JC13';
acctList.add(a);
i++;
}

// update the Accounts
update acctList;

}