my Trigger class code
trigger LeadTrigger on Lead (/*before insert,*/ after insert, after update) {
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
LeadTriggerHandler.createApplicationAfterLeadConverted(Trigger.new);
}
/* if(Trigger.isAfter && Trigger.isInsert){
for(Lead led: Trigger.new){
Contact newCnt=new Contact(Name= led.Student1_Name__c);
insert newCnt;
}
}*/
}
my trigger handler class
public class LeadTriggerHandler {
public static void createApplicationAfterLeadConverted(List<Lead> newLead){
List<hed__Application__c> AppList=/*[SELECT Id, FROM Application WHERE LeadId__c=:newLead[0].id];//*/new List<hed__Application__c>();
for(Lead Ld : newLead){
if(Ld.Status=='Qualified') /*(Ld.IsCorverted=true)*/{
hed__Application__c newApp = new hed__Application__c();
newApp.hed__Applicant__c=Ld.ConvertedContactId;
newApp.hed__Applying_To__c=Ld.ConvertedAccountId;
//OwnerId=Ld.OwnerId
AppList.add(newApp);
}
}
insert(AppList);
}
}
Guide me if you can thanks in advance.
The issue with your code is that you are not querying the AppList to see if a Lead was converted to an Application. Without querying the AppList and finding if a Lead was converted to an Application, the code will attempt to create a new Application for every Lead that is qualified or converted.
To fix this, you should query the AppList for every Lead in the newLead list and if a Lead was already converted to an Application, it should not be added to the AppList.
The corrected code should look like this:
public class LeadTriggerHandler {
public static void createApplicationAfterLeadConverted(List<Lead> newLead){
List<hed__Application__c> AppList=new List<hed__Application__c>();
for(Lead Ld : newLead){
// Query the AppList to see if the Lead was already converted
List<hed__Application__c> apps = [SELECT Id, FROM Application WHERE LeadId__c=:Ld.id];
if(apps.size() == 0 && Ld.Status=='Qualified'){ /*(Ld.IsCorverted=true)*/