You need to sign in to do that
Don't have an account?

trigger to convert a lead to contact
Hi everyone,
can anyone help me out in writing a trigger that automatically converts lead into contact.
While converting, I need to check whether lead email exists in existing contacts or not, if exists then I need to update contacts else I need to create a contact.
On lead I have Primary_Program field which is picklist, if it is not null then I need to create a record in Relationship object.
On relationship object, I need to populate the Program__c = Lead.Primary Program and
Contact__c = newly created contact in trigger.
I tried below code.
can anyone help me out in writing a trigger that automatically converts lead into contact.
While converting, I need to check whether lead email exists in existing contacts or not, if exists then I need to update contacts else I need to create a contact.
On lead I have Primary_Program field which is picklist, if it is not null then I need to create a record in Relationship object.
On relationship object, I need to populate the Program__c = Lead.Primary Program and
Contact__c = newly created contact in trigger.
I tried below code.
trigger ContactCreationFromLead on Lead (After insert) { 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<String> listEmail = new List<String>(); if(trigger.isInsert || trigger.isUpdate){ for (Lead em : Trigger.new) { if(em.Email != null){ listEmail.add(em.Email); } } } List<Contact> cem = [SELECT Id, Email FROM Contact WHERE Email = :listEmail]; String cemail; for(Contact ce : cem){ cemail = ce.Email; } 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); } else{ if(ld.Email != cemail && ld.Parent_or_guardian__c == true ){ Contact cnt1 = new Contact(); cnt1.RecordType.DeveloperName = '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.RecordType.DeveloperName = '012P00000005ZDpIAM'; cnt2.FirstName = ld.FirstName; cnt2.LastName = ld.LastName; cnt2.Email = ld.Email; conInsertList.add(cnt2); } } } if(conInsertList.size()>0){ INSERT conInsertList; List<Id> conInsert = new List<Id>(); for(Contact con : conInsertList){ conInsert.add(con.Id); } } }
I read your code as you did some mistake in your code Please try the given corrected code as it may help you in solving your problem.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha