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
Kamaldeep SehrawatKamaldeep Sehrawat 

System.FinalException: Record is read-only: Class.IAAAccountUpdate.AccountUpdate - Trigger

Hi All, 

I am getting the fololowing Error while creating the Class and the Trigger

Apex trigger IAAAccountupdate caused an unexpected exception,
contact your administrator: IAAAccountupdate: execution of AfterInsert caused by:
System.FinalException: Record is read-only: Class.IAAAccountUpdate.AccountUpdate: line 18, column 1



Below is the Code for my Apex Class 


public with sharing class IAAAccountUpdate
{
   
    private List<Contact> contactUpdateList;
   
    public IAAAccountUpdate()
    {
        contactUpdateList = new List<Contact>();
    }

    public void AccountUpdate(Contact[] triggersNew, Contact[] TriggersOld)
   {
        Account iaa = [Select Id FROM Account a WHERE Account_ID__c = 'gguiaa'];
        for (Contact contemp : triggersNew)
        {
                if  (contemp.Area_of_Interest_del__c != 'Law' && (contemp.Visa_Type__c == 'J1 Exchange Visitor'||contemp.Visa_Type__c =='F1 Nonimmigrant Student'||contemp.International_Student__c ==True))
                {
                    contemp.AccountId = iaa.id;
                   
                    contactUpdateList.add(contemp);
                }     
        }     
         update contactUpdateList;
    }
   
}



Apex Trigger

trigger IAAAccountupdate on Contact (After Insert,After Update)
{
IAAAccountUpdate iaa = new IAAAccountUpdate();
iaa.AccountUpdate(trigger.new, Trigger.old);
}


Can anybody please help me in resolve this error, I will really appreacite your help. 

Thanks in advance 

Kamaldeep


MaxPowerForceMaxPowerForce
You cannot update fields on records in trigger.new during the execution of an after trigger.  It looks like you can just use a before trigger:

trigger IAAAccountupdate on Contact (before Insert, before Update)

and then remove anything referencing the contactUpdateList as it is no longer necessary (since the field change occurs in memory before the record commit).