0

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';
        }
    }
}
1

Your trigger on Case should be before insert not after insert. You can not update records in an after trigger.

  • Thanks Bravo !!! I was confused whether we can perform update in after triggers .. You are right we can only update records in before triggers . But is there anyway to update a record after the record is inserted having after trigger associated with the inserted record !! – Tawseef Mar 3 '15 at 18:06
0

You can not use an after update trigger to update the same object again. This could potentially create an infinite trigger loop.

Try switching to a before update trigger to make the required change.

You can Refer :- https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.