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
Mohd NabeelMohd Nabeel 

t: execution of AfterUpdate caused by: System.FinalException: Record is read-only: What i am doing wrong???

trigger InsertContact on Account (after insert, after update) {
if( Trigger.isUpdate && Trigger.isAfter){
        AccountContact.AccountBillingModify(Trigger.new);
    }
}


//Handler class

public class AccountContact{
 public static void AccountBillingModify(List<Account> acctList){
        
        Map<Id, Account> accsWithContactMap = new Map<Id, Account>([select id, BillingPostalCode, (select Id, MailingPostalCode from Contacts) 
                                                                    from account
                                                                    where id in :acctList]);
        for (Account acc: acctList){
            for(Contact con : accsWithContactMap.get(acc.Id).Contacts){
                if(acc.BillingPostalCode != con.MailingPostalCode){                    
                    acc.OutofZip__c = true;
                }
            }
        }
    }
}

 
Avinash GangadhareAvinash Gangadhare
Hi @Mohd Nabeel -- Trigger.new become read-only in after context of a trigger. You can create a temporary object and add it to the list and update it after iteration. Something like..

List<Account> lstAccountToUpdate = new List<Account>();
for (Account acc: acctList){
    Account tempAccount = new Account (Id = acc.Id);
    for(Contact con : accsWithContactMap.get(acc.Id).Contacts){
           if(acc.BillingPostalCode != con.MailingPostalCode){
                  tempAccount .OutofZip__c = true;
            }
   }
   lstAccountToUpdate .add(tempAccount);
}
if(!lstAccountToUpdate.isEmpty()){
  update lstAccountToUpdate ;
}
Mohd NabeelMohd Nabeel
Invalid Data.
Review all error messages below to correct your data.
Apex trigger InsertContact caused an unexpected exception, contact your administrator: InsertContact: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0012v00002cJi2UAAS; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, InsertContact: maximum trigger depth exceeded Account trigger event AfterInsert Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate: []: Class.AccountContact.onBefore: line 18, column 1


Getting this error
Avinash GangadhareAvinash Gangadhare
Recursion causing this.. please add one static flag as a class member to avoid recursion.
Thanks.