You need to sign in to do that
Don't have an account?
Apex trigger yields "Record is read-only"
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?
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:
Message Edited by TehNrd on 01-10-2008 01:33 PM
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?
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
{
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;
}