function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sainath VenkatSainath Venkat 

trigger to create contact from lead

 Can anyone helps me out in writing a trigger that will create a contact from lead if lead email does not exists in contact email id. 
Once lead is created, I need to check whether exists in existing contacts if not then create a new contact
Meghna Vijay 7Meghna Vijay 7
Hi Sainath,

You can use trigger with after insert event, check if 'Email is not blank' , query out the existing contacts based on lead's Id and lead's email and if the query does not return any results then create a new contact.

Hope it helps, if it does mark it as solved.


Khan AnasKhan Anas (Salesforce Developers) 
Hi Sainath,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger CreateContact on Lead (after insert) {
    List<Contact> conInsertList = new List<Contact>();
    List<String> listEmail = new List<String>();
    for (Lead em : {
        if(em.Email != null){
    List<Contact> cem = [SELECT Id, Email FROM Contact WHERE Email = :listEmail];
    String cemail;
    for(Contact ce : cem){
        cemail = ce.Email;
    for(Lead ld : {
        if (ld.Email != cemail) {
            Contact cnt = new Contact();
            cnt.FirstName = ld.FirstName;
            cnt.LastName = ld.LastName;
        INSERT conInsertList;  

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Sainath VenkatSainath Venkat
Hi Khan Anas,

Hope you doing good and your solution worked for me.
What i exactly want to do is that, I need to convert a lead automatically using trigger and while converting I need to create a contact by checking whether lead email id exists in contacts email id or not, if exists I need to update the contacts, if not then I need to create a contact.
Can you please help me out in this issue if possible please.