I have created a trigger(after update) on Lead which will fire on status change and create a contact and custom object record.
Issue is that trigger is working but creating around 7 records of contact and custom object , i am not able to debug this , any help would be helpful.
Trigger:
Trigger CreateContact on Lead (after insert, after Update) {
if(Trigger.isInsert) {
Lead2TriggerHandler.createContact(Trigger.new);
}
if(Trigger.isUpdate) {
Lead2TriggerHandler.createContact(Trigger.new);
}
}
Class:
public class LeadTriggerHandler {
public static void createContact(List<Lead> leads) {
List<Application__c> app = new List<Application__c>();
List<Contact> contact = new List<Contact>();
for(Lead acc : leads){
if(acc.Status=='Interviewed'){
Contact con = new Contact(LastName = acc.lastname,
FirstName= acc.FirstName,
RecordTypeId='someid');
contact.add(con);
}
insert contact;
}
for(Lead acc : leads){
if(acc.Status=='Interviewed'){
Application__c a = new Application__c(Name = acc.Lead_Program_Name__c,
Program__c= acc.Program__c,
Last_Name__c= acc.LastName,
First_Name__c= acc.FirstName,
Program_Session__c= acc.Program_Session__c);
app.add(a);
}
insert app;
}
}
}