+ Start a Discussion
MigMig 

execution of AfterInsert caused by: System.Exception: Record is read-only:

Error when I save a record The trigger is triggered but with an error :  "execution of AfterInsert caused by: System.Exception: Record is read-only: "

I'm trying to make an update after insert. And I'm ADMIN and the record is not read-only. Did anybody had an error like this one ?
Vijay RautVijay Raut
Hi,

We cannot update record using Trigger.new context variable in after insert trigger.
To update record in after insert trigger, you have to use SOQL and DML Statement.

Hope this would help.

Regards,
V. R.
MigMig
Tkx for your answer. I did not knew that It's always good to know And finally I change my trigger and indeed it works ...
Have a nice day. Cheers

Mig
David DanzigDavid Danzig

I have a similar situation and have tried using a SOQL query and DML update function but am still getting the error message. Please see code below and give me any suggestions.

 

 

trigger insertPIOPrec on Contact (after insert, after update) 
{
for (Contact c:Trigger.new){
If (c.PI_Ongoing_Participation__c == null){
PI_Ongoing_Participation__c p = new PI_Ongoing_Participation__c(Contact__c = c.Id);
p.Contact__c = c.Id;
insert p;
List<PI_Ongoing_Participation__c> insertedpiops = [SELECT Id, Contact__c FROM PI_Ongoing_Participation__c p WHERE Contact__c = :c.id];
for (Integer i = 0; i < Trigger.new.size(); i++){
if (Trigger.new[i].PI_Ongoing_Participation__c == null){
Trigger.new[i].PI_Ongoing_Participation__c = insertedpiops[i].Id;
update Trigger.new[i];
}
else{}
}
// c.PI_Ongoing_Participation__c = p.Id;
// update c;


}
else {}
}
}