I have a scenario whenever there is a change in the value of the Custom field (Date_calculation__c) manually or by any automated process , I want to increment the number field by +1 (Number_Field_For_WorkFlow);

I am getting an error as follows:

Error:Apex trigger countNumberField caused an unexpected exception, contact your administrator: countNumberField: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Class.CompareOldAndNewValueForProcessBuider.populateNumberField: line 12, column 1

Why do I get this error? How can I fix it?

Code as below :

public class CompareOldAndNewValueForProcessBuider {

    public static void populateNumberField(List<Contract> NewValueList,Map<id,Contract> OldValueMap){

       List<Contract> listToBeUpdated = new List<Contract>();

        for(Contract con : NewValueList){
            Contract cont = new Contract();
            cont = OldValueMap.get(con.Id);
            if((con.Status == 'Activated') && con.Date_Calculation__c!= cont.Date_Calculation__c){
                con.Number_Field_For_WorkFlow__c = con.Number_Field_For_WorkFlow__c+1;

2 Answers 2


From Apex Triggers:

There are two types of triggers:

  • Before triggers are used to update or validate record values before they’re saved to the database.

  • After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.

If you are performing a field update on trigger records, the logic belongs in the before context and does not require further DML.


The record is locked/read-only because you are trying to update the record in After trigger. In after trigger you cant change the values in trigger.new. Use before trigger to make changes in trigger.new and that will also reduce dml.

If you are wondering as why you cant update trigger.new in after trigger? If it was allowed the trigger will keep firing itself and will go on a loop. I hope it helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .