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
MaggieSumitMaggieSumit 

Apex trigger AppLeadCreation caused an unexpected exception, contact your administrator: AppLeadCreation: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.AppLeadCreation: line 68, column 1

I have written an trigger for update application object based on new created record, in update logic its working but I am getting error on Insert Logic.
Apex trigger AppLeadCreation caused an unexpected exception, contact your administrator: AppLeadCreation: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.AppLeadCreation: line 68, column 1
Please check the below code and suggest me what's wrong with this?
  1. trigger AppLeadCreation on alu_Application__c (after insert,before update){
  2.     
  3.     // SOQL QUERY FOR ITERATES
  4.     private static List<Lead> leadsList= new List<Lead>();
  5.     private static Set<String> appEmails = new Set<String>();
  6.   
  7.     
  8.     // MAP OF IDs AND EMAIL ADDRESS OF LEAD
  9.     private static  Map<String,Id> mapIdsWithLeadEmail = new Map<String,Id>();
  10.     
  11.     // MAP OF IDs AND EMAIL ADDRESS OF APPLICATION
  12.     Map<Id,String> mapIdsWithAppEmail = new Map<Id,String>();
  13.    
  14.     // MAP OF LEAD IDs AND APPLICATION ID
  15.     Map<Id,Id> mapAppIdsEmailIds = new Map<Id,Id>();
  16.     
  17.     Map<Id,Lead> mapAppIdwithLeads = new Map<Id,Lead>();
  18.     
  19.     Map<Id,Lead> leadsToCreate = new Map<Id,Lead>();
  20.     
  21.     Map<Id,Lead> leadtoUpdate = new Map<Id,Lead>();     
  22.     
  23.     Map<Id,alu_Application__c> apptoUpdate = new Map<Id,alu_Application__c>(); 
  24.    // to prevent the recursion    
  25.     public static boolean isRecursive=true;
  26.     System.debug('mapIdsWithLeadEmail'+mapIdsWithLeadEmail);
  27.     
  28.     for(alu_Application__c app : trigger.new){
  29.         if(String.isNotBlank(app.Email__c)){
  30.            mapIdsWithAppEmail.put(app.Id,app.Email__c);
  31.            appEmails.add(app.Email__c);
  32.          }
  33.     }
  34.     System.debug('mapIdsWithAppEmail'+mapIdsWithAppEmail);
  35.     try {
  36.            if(!mapIdsWithAppEmail.isEmpty() && isRecursive){
  37.                isRecursive = false;
  38.               // fetch leads information based on Application Email
  39.               leadsList =[SELECT Id, Email FROM Lead WHERE Email IN: appEmails];
  40.               if(!leadsList.isEmpty()){
  41.                  for(Lead le : leadsList){       
  42.                    mapIdsWithLeadEmail.put(le.email, le.Id);
  43.               }
  44.              } 
  45.             
  46.             for(Id tmp : mapIdsWithAppEmail.KeySet()){
  47.                 String appEmail = mapIdsWithAppEmail.get(tmp);
  48.                if(mapIdsWithLeadEmail.ContainsKey(appEmail)){ 
  49.                    mapAppIdsEmailIds.put(tmp,mapIdsWithLeadEmail.get(appEmail));
  50.                }else{
  51.                     leadsToCreate.put(tmp,leadInfo(trigger.newMap.get(tmp),'insert',null));
  52.                }
  53.               }
  54.             }
  55.         System.debug('mapAppIdsEmailIds'+mapAppIdsEmailIds);
  56.         System.debug('leadsToCreate'+leadsToCreate);
  57.         
  58.         if(!mapAppIdsEmailIds.isEmpty() || !leadsToCreate.isEmpty()){
  59.             //insert the new leads for unmathced application EMAIL
  60.             Database.insert(leadsToCreate.values());
  61.             
  62.             for(alu_Application__c app : trigger.new){
  63.                 if(mapAppIdsEmailIds.ContainsKey(app.Id)){
  64.                     app.Lead__c = mapAppIdsEmailIds.get(app.Id);
  65.                     leadtoUpdate.put(app.Id, leadInfo(app,'update',app.Lead__c));
  66.                 }               
  67.                 else if(leadsToCreate.containsKey(app.Id)){
  68.                     app.Lead__c = leadsToCreate.get(app.Id).Id;
  69.                 }
  70.                 if(Trigger.isAfter && Trigger.isInsert && app.Lead__c != NULL){
  71.                     apptoUpdate.put(app.Id,app);
  72.                 }
  73.             }
  74.             if(!leadtoUpdate.isEmpty()){
  75.                 update leadtoUpdate.values();
  76.             }
  77.             if(!apptoUpdate.isEmpty()){
  78.                 update apptoUpdate.values();
  79.             }
  80.          }     
  81.         }Catch(Exception e){
  82.         System.debug('ERROR:' + e.getMessage());
  83.         System.debug('ERROR:' + e.getLineNumber());
  84.       }
  85.         
  86.      private static Lead leadInfo(alu_Application__c app, String eventType, Id LeadId){
  87.          Lead leadtoSend = new Lead();
  88.          if(eventType.equals('update')){
  89.              leadtoSend.Id = LeadId;
  90.          }
  91.          leadtoSend.FirstName = app.First_Name__c;
  92.          leadtoSend.LastName = app.Last_Name__c;
  93.          leadtoSend.Email = app.Email__c;
  94.          leadtoSend.Campus_Preference__c = app.Campus_Preference__c; 
  95.          leadtoSend.Company = app.First_Parent_Company__c;
  96.         
  97.          
  98.         return leadtoSend;
  99.      }
  100.     
  101. }
Paul S.Paul S.
You can't make changes to the records in Trigger.new.  Modify line 62 to this:
for(alu_Application__c app : [SELECT <fields> FROM alu_Application__c WHERE Id in :trigger.new])