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
paddupaddu 

System.FinalException: Record is read-only

Hi,

 

I'm getting the " Execution of AfterUpdate :System.FinalException: Record is read-only" while updating an Opportunity record. Any help must be highly appreciated.

 

Here is my trigger:

 

trigger updateOpportunity on Opportunity (before insert,after update) {
Set<Id> opportunityIds = new Set<Id>();

  for(Opportunity opp: Trigger.new){
 
   if(opp.Amount >=  5000){
          opp.newPro__c = true;
    }
 
   if(opp.Amount < 5000 && opp.HasOpportunityLineItem == true){
       opportunityIds.add(opp.Id);
      }
    }
 
  if(!opportunityIds.isEmpty())
            UpdateOpportunity_CLS.updateCheckBox(opportunityIds);
 }

 

Here is the class

Public class UpdateOpportunity_CLS{

public static void updateCheckBox(Set<id> oppIds)
{
    List<OpportunityLineItem> oppLineItems = New List<OpportunityLineItem>();
    Set<Id> OppIdYes = New Set<Id>();
    oppLineItems = [SELECT
                           Id,
                           OpportunityId,
                           Opportunity.Internal__C,
                           Opportunity.Amount,
                           PricebookEntry.Product2.Internal_Product__c
                    FROM
                           OpportunityLineItem       
                    WHERE
                           OpportunityId in : oppIds];
                           
                    
    for(OpportunityLineItem opl : oppLineItems)
    {
        if(opl.Opportunity.Amount >=5000 || opl.PricebookEntry.Product2.Internal_Product__c == true)
        {
            OppIdYes.add(opl.OpportunityId);
         }
    }
     
    List<opportunity> updateOpps = New List<opportunity>();
    List<opportunity> OppList = New List<opportunity>();
    OppList = [SELECT
                      Internal__C
               FROM
                      Opportunity
               WHERE
     
                      Id in : OppIdYes];

    for(opportunity Opp : OppList)
    {
        if(OppIdYes.contains(Opp.id) && opp.Internal__C != true)
        {
            opp.Internal__C = true;
           
            updateOpps.add(Opp);
        }
          
            
    }  
      if(!updateOpps.isEmpty())
        update updateOpps;       
                
}

 

 

Thanks in advance !!

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Change "after update" to "before update". You can't modify a record in a trigger in the after-dml event.

All Answers

sfdcfoxsfdcfox

Change "after update" to "before update". You can't modify a record in a trigger in the after-dml event.

This was selected as the best answer
paddupaddu

Thanks a lot.