Skip to main content
Subhadeep Das (TCS) a posé une question dans #Apex
Hi 

I am getting an error while executing the trigger :

trigger TestHelloWorld on Account (After insert) {

    System.debug('hello World After Insert of Account !!!! ');

    for(Account a : Trigger.New){

        a.Description = 'Created from Trigger';

    } 

}

Error :

FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TestHelloWorld: execution of AfterInsert

17:03:10:000 FATAL_ERROR caused by: System.FinalException: Record is read-only

While same is getting executed for Event : Before Insert

I am having System Admin profile and running thorugh Ann. Apex editor

Need to know the reason.. Please guide

 
4 réponses
  1. 5 janv. 2024, 12:47

    Because you cannot update the same record in After Insert context.

    Use Before context variables in updating the same records

    No need of DML in Before Insert 

    trigger TestHelloWorld on Account (Before insert)

    {

    System.debug('hello World After Insert of Account !!!! ');

    for(Account a : Trigger.New)

    {

    a.Description = 'Created from Trigger';

    }

    }

0/9000