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?
trigger AppLeadCreation on alu_Application__c (after insert,before update){
// SOQL QUERY FOR ITERATES
private static List<Lead> leadsList= new List<Lead>();
private static Set<String> appEmails = new Set<String>();
// MAP OF IDs AND EMAIL ADDRESS OF LEAD
private static Map<String,Id> mapIdsWithLeadEmail = new Map<String,Id>();
// MAP OF IDs AND EMAIL ADDRESS OF APPLICATION
Map<Id,String> mapIdsWithAppEmail = new Map<Id,String>();
// MAP OF LEAD IDs AND APPLICATION ID
Map<Id,Id> mapAppIdsEmailIds = new Map<Id,Id>();
Map<Id,Lead> mapAppIdwithLeads = new Map<Id,Lead>();
Map<Id,Lead> leadsToCreate = new Map<Id,Lead>();
Map<Id,Lead> leadtoUpdate = new Map<Id,Lead>();
Map<Id,alu_Application__c> apptoUpdate = new Map<Id,alu_Application__c>();
// to prevent the recursion
public static boolean isRecursive=true;
System.debug('mapIdsWithLeadEmail'+mapIdsWithLeadEmail);
for(alu_Application__c app : trigger.new){
if(String.isNotBlank(app.Email__c)){
mapIdsWithAppEmail.put(app.Id,app.Email__c);
appEmails.add(app.Email__c);
}
}
System.debug('mapIdsWithAppEmail'+mapIdsWithAppEmail);
try {
if(!mapIdsWithAppEmail.isEmpty() && isRecursive){
isRecursive = false;
// fetch leads information based on Application Email
leadsList =[SELECT Id, Email FROM Lead WHERE Email IN: appEmails];
if(!leadsList.isEmpty()){
for(Lead le : leadsList){
mapIdsWithLeadEmail.put(le.email, le.Id);
}
}
for(Id tmp : mapIdsWithAppEmail.KeySet()){
String appEmail = mapIdsWithAppEmail.get(tmp);
if(mapIdsWithLeadEmail.ContainsKey(appEmail)){
mapAppIdsEmailIds.put(tmp,mapIdsWithLeadEmail.get(appEmail));
}else{
leadsToCreate.put(tmp,leadInfo(trigger.newMap.get(tmp),'insert',null));
}
}
}
System.debug('mapAppIdsEmailIds'+mapAppIdsEmailIds);
System.debug('leadsToCreate'+leadsToCreate);
if(!mapAppIdsEmailIds.isEmpty() || !leadsToCreate.isEmpty()){
//insert the new leads for unmathced application EMAIL
Database.insert(leadsToCreate.values());
for(alu_Application__c app : trigger.new){
if(mapAppIdsEmailIds.ContainsKey(app.Id)){
app.Lead__c = mapAppIdsEmailIds.get(app.Id);
leadtoUpdate.put(app.Id, leadInfo(app,'update',app.Lead__c));
}
else if(leadsToCreate.containsKey(app.Id)){
**app.Lead__c = leadsToCreate.get(app.Id).Id;**
}
if(Trigger.isAfter && Trigger.isInsert && app.Lead__c != NULL){
apptoUpdate.put(app.Id,app);
}
}
if(!leadtoUpdate.isEmpty()){
update leadtoUpdate.values();
}
if(!apptoUpdate.isEmpty()){
update apptoUpdate.values();
}
}
}Catch(Exception e){
System.debug('ERROR:' + e.getMessage());
System.debug('ERROR:' + e.getLineNumber());
}
private static Lead leadInfo(alu_Application__c app, String eventType, Id LeadId){
Lead leadtoSend = new Lead();
if(eventType.equals('update')){
leadtoSend.Id = LeadId;
}
leadtoSend.FirstName = app.First_Name__c;
leadtoSend.LastName = app.Last_Name__c;
leadtoSend.Email = app.Email__c;
leadtoSend.Campus_Preference__c = app.Campus_Preference__c;
leadtoSend.Company = app.First_Parent_Company__c;
return leadtoSend;
}
}