I have created after insert trigger on Account which creates a new case when an account is created, I have created another after trigger on case and I am trying to update the field value or set any field in the case. While trying so I am getting the error:
Error: Invalid Data. Review all error messages below to
correct your data. Apex trigger AccountTrigger caused an unexpected
exception, contact your administrator: AccountTrigger: execution of
AfterInsert caused by: System.DmlException: Insert failed. First
exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
caseTrigger: execution of AfterInsert caused by:
System.FinalException: Record is read-only Trigger.caseTrigger: line
4, column 1: []: Trigger.AccountTrigger: line 7, column 1.
Can u please let me know the reason ! Thanking in Advance
Trigger on Account:
trigger AccountTrigger on Account(after insert) {
for (Account cc : trigger.new){
Case contactCase = new Case();
contactCase.priority = 'Ultra High';
contactCase.subject = 'Send them a free T shirt';
contactCase.AccountId= cc.id;
insert contactCase;
}
}
Trigger on Case:
trigger caseTrigger on Case (after insert) {
for ( Case c : Trigger.new){
if (c.SuppliedCompany== NULL){
c.SuppliedCompany ='this is my new company';
}
}
}
Best Answer
Your trigger on
Case
should bebefore insert
notafter insert
. You can not update records in an after trigger.