You need to sign in to do that
Don't have an account?
Saurav 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
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
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:
Hope this helps! Please mark as best if it does
Thanks
All Answers
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:
Hope this helps! Please mark as best if it does
Thanks