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

Trigger Read Only Exception not encountered. Why?
Hi,
I have the following trigger:
trigger conCreateFromAccount on Account (after insert) {
List<Contact> conList = new List<Contact>();
for (Account newAccount: Trigger.New) {
Contact con = new Contact();
con.lastname = newAccount.name + ' - Contact';
con.Accountid = newAccount.id;
conList.add(con);
}
insert conList;
list<account>acctsUp = new list<account>();
for(contact e: conList){
account a = new account(Id = e.AccountId);
a.Contact_ID__c = e.Id;
acctsUp.add(a);
}
update acctsUp;
}
From what I know, records in AFTER are read-only and cannot be updated. However, I am able to update the account record in the after insert trigger without any issue. Theoretically I should be receiving the Read Only error, but I am not. Why?
I have the following trigger:
trigger conCreateFromAccount on Account (after insert) {
List<Contact> conList = new List<Contact>();
for (Account newAccount: Trigger.New) {
Contact con = new Contact();
con.lastname = newAccount.name + ' - Contact';
con.Accountid = newAccount.id;
conList.add(con);
}
insert conList;
list<account>acctsUp = new list<account>();
for(contact e: conList){
account a = new account(Id = e.AccountId);
a.Contact_ID__c = e.Id;
acctsUp.add(a);
}
update acctsUp;
}
From what I know, records in AFTER are read-only and cannot be updated. However, I am able to update the account record in the after insert trigger without any issue. Theoretically I should be receiving the Read Only error, but I am not. Why?
In a after insert trigger it is not allowed change field using trigger.new. This is the right and complete statement.
In your code if you had used soemthing like this then you will get the read-only error.
Let me know if you have any doubts.
Thanks!
All Answers
In a after insert trigger it is not allowed change field using trigger.new. This is the right and complete statement.
In your code if you had used soemthing like this then you will get the read-only error.
Let me know if you have any doubts.
Thanks!
because you are not updating any account in the below for loop you are not getting 'Read only exception' but if you do something like this you will get 'Read only exception'