+ Start a Discussion
William Woodson 3William Woodson 3 

before update trigger read only error

I cannot figure out why this code is throwing the following error...
System.FinalException: Record is read-only

This code is in a helper method that receives a single object from the Trigger.New value and will alway be a single object not bulk.  Error line appears to be this bit of code "hr.Date_Interviewed__c = date.today();"  The trigger fired is a before update...

if (Trigger.isBefore && Trigger.isUpdate) {
Hiring_Audit__c hr = (Hiring_Audit__c) so;
if(hr.Stage__c == 'Submittal') {
hr.Date_Submitted__c = date.today();
}
else if(hr.Stage__c == 'Interview') {
hr.Date_Interviewed__c = date.today();
}
else if(hr.Stage__c == 'Offered') {
hr.Date_Offered__c = date.today();
}
else {
hr.Date_Applied__c = date.today();
}
update hr;
}


 
Best Answer chosen by William Woodson 3
Maharajan CMaharajan C
HI William,

how you are setting the Hiring_Audit__c record in      ==>    Hiring_Audit__c hr = (Hiring_Audit__c) so ==> from Trigger.New() or Trigger.Old();

If you are using the Trigger.Old() to set the value in hr then change it as Trigger.New() because the Trigger.Old() also read only in before update.
  

All Answers

Raj VakatiRaj Vakati
Use this code
 
if (Trigger.isBefore && Trigger.isUpdate) {
Hiring_Audit__c hr = (Hiring_Audit__c) so;
if(hr.Stage__c == 'Submittal') {
hr.Date_Submitted__c = date.today();
}
else if(hr.Stage__c == 'Interview') {
hr.Date_Interviewed__c = date.today();
}
else if(hr.Stage__c == 'Offered') {
hr.Date_Offered__c = date.today();
}
else {
hr.Date_Applied__c = date.today();
}

}

 
Raj VakatiRaj Vakati
You no need to specify update DML in before update 
William Woodson 3William Woodson 3
I removed the update line and that did not fix the issue.
Raj VakatiRaj Vakati
Can you check the Debug logs and give me debug logs ?? 
 
Maharajan CMaharajan C
HI William,

how you are setting the Hiring_Audit__c record in      ==>    Hiring_Audit__c hr = (Hiring_Audit__c) so ==> from Trigger.New() or Trigger.Old();

If you are using the Trigger.Old() to set the value in hr then change it as Trigger.New() because the Trigger.Old() also read only in before update.
  
This was selected as the best answer
William Woodson 3William Woodson 3
Maharajan, you are right, further up my framework I had transposed the so and oldSo params in the method call and because they are both SObjects the compiler didn't see it.

Thank you so much, that was driving me crazy!