+ Start a Discussion
PrazPraz 

System.FinalException: Record is read-only

Hi,

 

I have written a trigger and the error is comng at the line it is highlighted

 

The fields here highlighted are the lookup of User object.

 

 

trigger UpdateMarkRequestTrigger on Marketing_Request__c (before update) {
if(Trigger.isBefore){
if(Trigger.isUpdate){
for(Marketing_Request__c mr:Trigger.old){
System.debug(Logginglevel.INFO,mr.Name);
if(mr.Status__c=='Review Completed')
System.debug(Logginglevel.INFO,mr.Status__c);
System.debug(Logginglevel.INFO,mr.Project_Owner__c);
System.debug(Logginglevel.INFO,mr.Reassign_To__c);

mr.Reassign_To__c=mr.Project_Owner__c;

System.debug(Logginglevel.INFO,mr.Project_Owner__c);
System.debug(Logginglevel.INFO,mr.Reassign_To__c);
}
}
}

 

 

Help will be appreciated

 

Regards

 

Praz

hisrinuhisrinu

You have to use Trigger.New not the Trigger.old

 

 

trigger UpdateMarkRequestTrigger on Marketing_Request__c (before update) {
if(Trigger.isBefore){
if(Trigger.isUpdate){
for(Marketing_Request__c mr:Trigger.NEW){
System.debug(Logginglevel.INFO,mr.Name);
if(mr.Status__c=='Review Completed')
System.debug(Logginglevel.INFO,mr.Status__c);
System.debug(Logginglevel.INFO,mr.Project_Owner__c);
System.debug(Logginglevel.INFO,mr.Reassign_To__c);

mr.Reassign_To__c=mr.Project_Owner__c;

System.debug(Logginglevel.INFO,mr.Project_Owner__c);
System.debug(Logginglevel.INFO,mr.Reassign_To__c);
}
}
}

PrasenPrasen

But this is an existing record...

_Prasu__Prasu_
Trigger.old values are always readonly.
hisrinuhisrinu

Trigger.old means these will capture the previous values (before change) and trigger.new will capture the new values what user has updated

rick82000rick82000

Can you make the trigger to "After update" instead of "before update." I don't see the reason why the trigger is before update. This should resolve the issue.


hisrinu wrote:

Trigger.old means these will capture the previous values (before change) and trigger.new will capture the new values what user has updated


 

rick82000rick82000

Sorry for the half answer before.

 

Here is the code that i would recommend

 

trigger UpdateMarkRequestTrigger on Marketing_Request__c (after update) {
// if(Trigger.isBefore){
if(Trigger.isUpdate){
for(Marketing_Request__c mr:Trigger.old){
System.debug(Logginglevel.INFO,mr.Name);
if(mr.Status__c=='Review Completed')
System.debug(Logginglevel.INFO,mr.Status__c);
System.debug(Logginglevel.INFO,mr.Project_Owner__c);
System.debug(Logginglevel.INFO,mr.Reassign_To__c);

mr.Reassign_To__c=mr.Project_Owner__c;

System.debug(Logginglevel.INFO,mr.Project_Owner__c);
System.debug(Logginglevel.INFO,mr.Reassign_To__c);
}
// }
}
PrazPraz

same exception!!!

_Prasu__Prasu_
trigger UpdateMarkRequestTrigger on Marketing_Request__c (before update) {
// if(Trigger.isBefore){
if(Trigger.isUpdate){
for(Marketing_Request__c mr:Trigger.new){
System.debug(Logginglevel.INFO,mr.Name);
if(mr.Status__c=='Review Completed')
System.debug(Logginglevel.INFO,mr.Status__c);
System.debug(Logginglevel.INFO,mr.Project_Owner__c);
System.debug(Logginglevel.INFO,mr.Reassign_To__c);

mr.Reassign_To__c=mr.Project_Owner__c;

System.debug(Logginglevel.INFO,mr.Project_Owner__c);
System.debug(Logginglevel.INFO,mr.Reassign_To__c);
}
// }
}