I have trigger on Lead object which is creating Contacts, records in Relationship objects, affiliation objects etc.
On lead creation my trigger is working perfectly but I want to convert lead automatically before creating all the records. right now if I use database.lead converts then its creating two contacts but I want only one contact to get created.
Can anyone helps me out in this issue.
trigger ContactCreationFromLead on Lead (After insert, after Update) {
//trigger to create contact, relationship, affiliation records from lead.
List<Lead> em = new List<Lead>();
List<Contact> conInsertList = new List<Contact>();
List<hed__Affiliation__c> affiliation = new List <hed__Affiliation__c>();
List<hed__Relationship__c> relation = new List<hed__Relationship__c>();
List<hed__Affiliation__c> affiliationListToBeInserted = new List<hed__Affiliation__c>();
List<String> listEmail = new List<String>();
List<String> listname = new List<String>();
Set<String> otherProgramsValueSet = new Set<String>();
if(trigger.isInsert || trigger.isUpdate){
for (Lead em : Trigger.new) {
if(em.Email != null){
listEmail.add(em.Email);
}
if(em.What_is_your_primary_program_of_interest__c != null ){
listname.add(em.What_is_your_primary_program_of_interest__c);
}
if(em.Other_Programs_of_Interest__c != null){
otherProgramsValueSet.addAll(em.Other_Programs_of_Interest__c.split(';')) ;
}
}
}
//to catch the account Id that matches the lead primary program of interest
//account name should match with primary program of interest picklist values
List<Account> account = [SELECT Id,Name,Pardot_Program_Name__c from Account WHERE Pardot_Program_Name__c = :listname];
String record;
for(Account acc : account){
record = acc.Id;
}
List<Contact> cem = [SELECT Id, Email FROM Contact WHERE Email = :listEmail];
String cemail;
for(Contact ce : cem){
cemail = ce.Email;
}
//code to create contact for student if form is filled by student
for(Lead ld : Trigger.new) {
if (ld.Email != cemail && ld.Parent_or_guardian__c == false) {
Contact cnt = new Contact();
cnt.FirstName = ld.FirstName;
cnt.LastName = ld.LastName;
cnt.Email = ld.Email;
cnt.RecordTypeId = '012P00000005ZDpIAM';
conInsertList.add(cnt);
}
//code to create contacts for parent and student if form is filled by parent
else{
if(ld.Email != cemail && ld.Parent_or_guardian__c == true ){
Contact cnt1 = new Contact();
cnt1.RecordTypeId = '012P00000005ZDuIAM';
cnt1.FirstName = ld.Parent_First_Name__c;
cnt1.LastName = ld.Parent_Last_Name__c;
cnt1.Email = ld.Parent_Email__c;
conInsertList.add(cnt1);
Contact cnt2 = new Contact();
cnt2.RecordTypeId = '012P00000005ZDpIAM';
cnt2.FirstName = ld.FirstName;
cnt2.LastName = ld.LastName;
cnt2.Email = ld.Email;
cnt2.HasOptedOutOfEmail = ld.Email_Opt_Out__c;
conInsertList.add(cnt2);
}
}
}
if(conInsertList.size()>0){
INSERT conInsertList;
List<Id> conInsert = new List<Id>();
//catch the inserted contact ids to create relationship record
if(conInsertList.size()>1){
for(Integer i = 0, s = conInsertList.size(); i < s; i += 2){
//creates relation record by catching current student and parent record id's
hed__Relationship__c hedrel = new hed__Relationship__c();
hedrel.hed__Contact__c = conInsertList[i + 1].Id;
hedrel.hed__RelatedContact__c = conInsertList[i].Id;
hedrel.hed__Type__c = 'Parent';
relation.add(hedrel);
//creates affiliation record if filled by parent
hed__Affiliation__c hedaff = new hed__Affiliation__c();
hedaff.hed__Account__c = record;
hedaff.hed__Contact__c = conInsertList[i + 1].Id;
hedaff.hed__Primary__c = true;
affiliation.add(hedaff);
//creates affiliation records for other programs multi select picklist field
for(Account acc : [select id from account where name in : otherProgramsValueSet]){
affiliationListToBeInserted.add(new hed__Affiliation__c(hed__Account__c= acc.Id,
hed__Contact__c=conInsertList[i + 1].Id));
}
}
}
else{
//creates Affiliation record if filled by student only
if(conInsertList.size()==1){
for(Integer i = 0, s = conInsertList.size(); i < s; i += 2){
hed__Affiliation__c hedaff = new hed__Affiliation__c();
hedaff.hed__Account__c = record ;
hedaff.hed__Contact__c = conInsertList[i].Id;
hedaff.hed__Primary__c = true;
affiliation.add(hedaff);
for(Account acc : [select id from account where Pardot_Program_Name__c in : otherProgramsValueSet]){
affiliationListToBeInserted.add(new hed__Affiliation__c(hed__Account__c= acc.Id,
hed__Contact__c=conInsertList[i].Id,
hed__Role__c='Student'));
}
}
}
}
insert relation;
insert affiliation;
insert affiliationListToBeInserted;
}
LeadStatus convertStatus = [Select MasterLabel from LeadStatus where IsConverted= true limit 1];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'New' ) {
Database.LeadConvert lc = new Database.LeadConvert();
String opportunityName = lead.Name;
lc.setLeadId(lead.Id);
lc.setSendNotificationEmail(false);
lc.setDoNotCreateOpportunity(true);
lc.setConvertedStatus(convertStatus.MasterLabel);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}