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
Shavi DabgotraShavi Dabgotra 

Error:Apex trigger ASCExists caused an unexpected exception, contact your administrator: ASCExists: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.ASCExists: line 6, column 1

I have a requirement in which when the Billing State is Punjab then a custom checkbox(Exists__c should be true) on Account Object. Billing State is a picklist.
I have used Trigger to update that.
trigger ASCExists on Account (after update) {
    if(trigger.isAfter && trigger.isUpdate){
        for(Account acc:trigger.new){
            if(acc.BillingCountry == 'India' && acc.BillingState == 'Punjab' )
            {
                acc.Exists__c = true ;
                update acc;
            }
        }
    }
}
But I am getting this error as:
Error:Apex trigger ASCExists caused an unexpected exception, contact your administrator: ASCExists: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.ASCExists: line 6, column 1

How to fix this. Can somebody help in this.
Thank you 
Best Answer chosen by Shavi Dabgotra
sachinarorasfsachinarorasf
Hi Shavi,

Please try this regarding your requirement.
trigger asc_exists on Account (before update, before insert) {
    if(trigger.isBefore && (trigger.isUpdate || trigger.isInsert)){
        for(Account acc : trigger.new){
            if(acc.BillingState == 'Punjab')
            {
                acc.Advance_Solutions_Exists__c = false;
            }
            else{
                acc.Advance_Solutions_Exists__c = true;
            }
            
        }    
    }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com

All Answers

VinayVinay (Salesforce Developers) 
Can you check FLS of 'Exists__c ' field?
sachinarorasfsachinarorasf
Hi Shavi,

Please use the before update trigger rather than after update trigger. 
In after the update record is locked and you can not update it.

Please go through the following link for a similar issue and solution.

https://salesforce.stackexchange.com/questions/190358/error-within-trigger-system-finalexception-record-is-read-only
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
Shavi DabgotraShavi Dabgotra
Hi Sachin,
This solved my problem thank you.
But one problem came as: When Billing state is Punjab it is setting a value to False otherwise, set to True.

trigger asc_exists on Account (before update, before insert) {
    if(trigger.isBefore && (trigger.isUpdate || trigger.isInsert)){
        for(Account acc : trigger.new){
            if(acc.BillingState == 'Punjab')
            {
                acc.Advance_Solutions_Exists__c = true;
            }
            else{
                acc.Advance_Solutions_Exists__c = false;
            }
            
        }    
    }
}

Thank you in advance.
sachinarorasfsachinarorasf
Hi Shavi,

Please try this regarding your requirement.
trigger asc_exists on Account (before update, before insert) {
    if(trigger.isBefore && (trigger.isUpdate || trigger.isInsert)){
        for(Account acc : trigger.new){
            if(acc.BillingState == 'Punjab')
            {
                acc.Advance_Solutions_Exists__c = false;
            }
            else{
                acc.Advance_Solutions_Exists__c = true;
            }
            
        }    
    }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
This was selected as the best answer