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
SFDC DummySFDC Dummy 

execution of AfterInsert caused by: System.FinalException: Record is read-only:

Hi Friends 

How to achieve this type of error in the bellow trigger..

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger realtime2 caused an unexpected exception, contact your administrator: realtime2: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.realtime2: line 8, column 1

 
trigger realtime2 on BankBook__c (after insert)
{
   
  
        for(BankBook__c stuObj:Trigger.new)
        {
          
            stuObj.New_Closing_Bal__c= stuObj.New_Related_Bank_Acc__c;
            }
        }

 
Praveen Venkata BPraveen Venkata B
You are trying to update the same record after it was inserted. You need to change your trigger event to before insert to overcome this error.
Amit Chaudhary 8Amit Chaudhary 8

Please try below code:-
trigger realtime2 on BankBook__c (before insert)
{
   
  
        for(BankBook__c stuObj:Trigger.new)
        {
          
            stuObj.New_Closing_Bal__c= stuObj.New_Related_Bank_Acc__c;
         }
}
NOte:- If you want to update the same record then please use before insert


Please check below blog for more information on trigger framwork
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please check below post:- Salesforce Trigger: When to use Before versus After
http://raydehler.com/cloud/clod/salesforce-trigger-when-to-use-before-versus-after.html

Please let us know if this will help u

Thanks
Amit Chaudhary
ManojjenaManojjena
Hi SFDC Dummy,

Amit and Praveen are correct you can achieve this by changing your event from after to before .
Basically you got this error because incase of after event your record is saved to Database you you can not assign value like before .
In this case you need to have queryu with all ids and need to update .

I think you are clear about your error now .

Try with below code it will solve your probelm as well .
trigger realtime2 on BankBook__c (after insert){
List<BankBook__c> bnkBookListToUpdate=new List<BankBook__c>();
   for(BankBook__c bkObj:[SELECT Id,New_Closing_Bal__c,New_Related_Bank_Acc__c  FROM BankBook__c WHERE Id IN :Trigger.new]){
        bkObj.New_Closing_Bal__c= bkObj.New_Related_Bank_Acc__c;
        bnkBookListToUpdate.add(bkObj);
    }
    try{
     update  bnkBookListToUpdate;
    }catch(DMlException de ){
      System.debug(de );
    }
}
Let me knwo if it helps !!
Thanks 
Manoj
 
SFDC DummySFDC Dummy
I have tried this code trigger realtime2 on BankBook__c (after insert){ List bnkBookListToUpdate=new List(); for(BankBook__c bkObj:[SELECT Id,New_Closing_Bal__c,New_Related_Bank_Acc__c,Debit__c,Credit__c FROM BankBook__c WHERE Id IN :Trigger.new]) { double s = bkObj.New_Related_Bank_Acc__c; //copy the notes to a string object double s1 = bkObj.Debit__c; double s2 = bkObj.Credit__c; bkObj.New_Closing_Bal__c=s-s1+s2 ; // bkObj.New_Closing_Bal__c= bkObj.New_Related_Bank_Acc__c; bnkBookListToUpdate.add(bkObj); } try{ update bnkBookListToUpdate; }catch(DMlException de ){ System.debug(de ); } } but its throwing this error Error: Invalid Data. Review all error messages below to correct your data. Apex trigger realtime2 caused an unexpected exception, contact your administrator: realtime2: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.realtime2: line 10, column 1
ManojjenaManojjena
Hi SFDC Dummy ,

I have checked your code .You need to modify the 2nd lin elike belwo .

List<BankBook__c> bnkBookListToUpdate=new List<BankBook__c>();

Also if you have to check the null for New_Related_Bank_Acc__c,Debit__c,Credit__c before calulation .
I can suggest you to create a formula fields to calculate this if you have any issue then just check Like belwo

if(bkObj.New_Related_Bank_Acc__c != null && bkObj.Debit__c != null && bkObj.Credit__c != null ){
bkObj.New_Closing_Bal__c=bkObj.New_Related_Bank_Acc__c-bkObj.Debit__c+bkObj.Credit__c;

}

LIke this you can modify but you have to check the null value .