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
Dev AggarwalDev Aggarwal 

'Record is read-only' error on updating a field before deleting

I have a custom field 'number_of_contact__c' in Account. I am trying to set the value of this custom field to zero before deleting the record.
I am getting an error at line ' loopAccount.number_of_contact__c = 0;'.
 
trigger RemoveNumberofcontact on Account (after undelete, before delete) 
if (Trigger.isDelete == true)
{	List <Account> ListAccount = new List<Account>(Trigger.old);
 
	for (Account LoopAccount : ListAccount)    
    {
        loopAccount.number_of_contact__c = 0;
    }
 update(ListAccount);
}
  
}

Thanks
Anil JAdhav 14Anil JAdhav 14
Hi Dev,
There are two scenario:
1. Please check field level security for that field.

2. I have done some changes please try to execute and let me know.
List <Account> ListAccount = new List<Account>(Trigger.old); - You are trying update trigger.old list. So you cannot make changes in old its always been read only.

trigger RemoveNumberofcontact on Account (after undelete, before delete) 
List<Account> UpdateListAccount = new List<Account>();
if (Trigger.isDelete == true)
{    List <Account> ListAccount = new List<Account>(Trigger.old);
 
    for (Account LoopAccount : ListAccount)    
    {
        loopAccount.number_of_contact__c = 0;
        UpdateListAccount.add(loopAccount);
    }
 update UpdateListAccount;
}
  
}

please let me know if it works or not.
Dev AggarwalDev Aggarwal
Hi Anil,
Thanks for your help however I am still facing the same issue.

1. I have Verifed field Level Security. there is nothing that may conflict with this trigger. 
2. I have Verified that no workflow, validation is conflicting with this update.
3. I am still getting the same error on running your code. I also trying modifying the trigger (Code mentioned below) but everything is in vain.
List<Account> UpdateListAccount = new List<Account>();
if (Trigger.isDelete == true)
{    List <Account> ListAccount = new List<Account>(Trigger.old);
 
    for (Account LoopAccount : ListAccount)    
    {
        
        UpdateListAccount.add(loopAccount);
    }
 for (Account looper : UpdateListAccount)
 {
     looper.number_of_contact__c = 0;
 }
 update UpdateListAccount;


 
Anil JAdhav 14Anil JAdhav 14
Hi Dev,

Just clear one thing, on which event you want to fire the chunk of code after undelete or before delete?
Anil JAdhav 14Anil JAdhav 14
Try this....

trigger RemoveNumberofcontact on Account (after undelete, before delete) {

     List<Account> UpdateListAccount = new List<Account>();
     if(Trigger.isBefore){
     if (Trigger.isDelete)
     {    
         List <Account> ListAccount = new List<Account>(Trigger.old);
        for (Account LoopAccount : ListAccount)    
        {
           loopAccount.number_of_contact__c = 0;
           UpdateListAccount.add(loopAccount);
        }
     update UpdateListAccount ;
       }
   }
  
}

also check by commenting update UpdateListAccount ; line