function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saurav Roy 15Saurav Roy 15 

Record is read-only on Before Trigger

Hi All,

Can anyone please help me. I am new to Salesforce.
I am getting Record read-only error while trying to update a field in Account object in a before trigger. 
Created a new field as Previous_Type__c which will store the Previous value of Account Type.
Trigger: 
trigger AccountTypeUpdate on Account (before update) {
    
    if(Trigger.isBefore && Trigger.isUpdate){
        if(AccountTypeUpdateClass.isTriggerExecuted){
                   AccountTypeUpdateClass.isTriggerExecuted = false;
        
                for(Account Acc: Trigger.old){
                    AccountTypeUpdateClass.ChangeType(Acc);   
                }  
        }
    }
}
Apex Class:
public class AccountTypeUpdateClass {
    public static boolean isTriggerExecuted = True;  
        public static void ChangeType(Account obj)
        {
                obj.Previous_Type__c = obj.Type;
        } 
}

Error Message:
AccountTypeUpdate: execution of BeforeUpdate caused by: System.FinalException: Record is read-only Class.AccountTypeUpdateClass.ChangeType: line 5, column 1 Trigger.AccountTypeUpdate: line 8, column 1
Best Answer chosen by Saurav Roy 15
SarvaniSarvani
Hi Saurav,

In your code you are trying to update the value of older version of record using Trigger.old list which is why you are seeing the record is read-only error. You can only set the values of fields for trigger.new list which is not passed to your class. 

Please try something like below:
trigger AccountTypeUpdate on Account (before update) {
    
    if(Trigger.isBefore && Trigger.isUpdate){
        if(AccountTypeUpdateClass.isTriggerExecuted){
                   AccountTypeUpdateClass.isTriggerExecuted = false;
            
                 for(Account Acc: Trigger.new){
                    string oldvalue =trigger.oldmap.get(Acc.id).Type;
                    AccountTypeUpdateClass.ChangeType(Acc,oldvalue);  // passing newlist and old value in Type field before updating the record 
                }  
          }
    }
}
public class AccountTypeUpdateClass {
    public static boolean isTriggerExecuted = True;  
        public static void ChangeType(Account Obj, string oldvalue)
        {   
           obj.Previous_Type__c = oldvalue;
         }
   }


Hope this helps! Please mark as best if it does

Thanks

All Answers

SarvaniSarvani
Hi Saurav,

In your code you are trying to update the value of older version of record using Trigger.old list which is why you are seeing the record is read-only error. You can only set the values of fields for trigger.new list which is not passed to your class. 

Please try something like below:
trigger AccountTypeUpdate on Account (before update) {
    
    if(Trigger.isBefore && Trigger.isUpdate){
        if(AccountTypeUpdateClass.isTriggerExecuted){
                   AccountTypeUpdateClass.isTriggerExecuted = false;
            
                 for(Account Acc: Trigger.new){
                    string oldvalue =trigger.oldmap.get(Acc.id).Type;
                    AccountTypeUpdateClass.ChangeType(Acc,oldvalue);  // passing newlist and old value in Type field before updating the record 
                }  
          }
    }
}
public class AccountTypeUpdateClass {
    public static boolean isTriggerExecuted = True;  
        public static void ChangeType(Account Obj, string oldvalue)
        {   
           obj.Previous_Type__c = oldvalue;
         }
   }


Hope this helps! Please mark as best if it does

Thanks
This was selected as the best answer
Saurav Roy 15Saurav Roy 15
Thank you Sarvani.