You need to sign in to do that
Don't have an account?
Dev 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;'.
Thanks
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
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.
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.
Just clear one thing, on which event you want to fire the chunk of code after undelete or before delete?