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
Zaide OstrozubiZaide Ostrozubi 

Prevent a lead created via web to lead if contact (email) already exists

Hi - totally new to code here and need help.  We are using web to leads for forms on our website.  Often our clients that already exist will complete a form and then the web to lead form creates a new lead.  I don't want this to happen.  I want the system to check if the "lead" already exists in Salesforce and create a task for follow up instead of creating a new lead.  How can I do this?  I've exhausted the salesforce support and they referred me to this page to ask for help from this fine community.  
ShirishaShirisha (Salesforce Developers) 
Hi Zaide,

Greetings!

We have an existing idea created for the same requirement to check for the duplicate leads and not to allow inserting the new lead with the same email address.

Idea:https://trailblazer.salesforce.com/ideaView?id=08730000000BpWE

However,you can merge the existing Lead/Contact with the newly created lead record to avoid duplicates.

Please use the sample code provided in the below thread:

https://salesforce.stackexchange.com/questions/293825/create-a-trigger-to-merge-leads-if-the-email-already-exists-on-a-lead

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Zaide OstrozubiZaide Ostrozubi
Yes I've alredy upvoted the idea and commented.  The issue is not if the lead already exists but to cross check if the "lead" person submitted a form via web to lead already exists in Salesforce.  If this person already exists as a contact then don't create a lead but create a task for follow up.  If the "lead" person submitting the form does not exist as a lead or contact then create a new lead.  
Dushyant SonwarDushyant Sonwar
Zaide,

You need to create a trigger to accomplish this task
I have created a trigger rough code for you , there may be typo mistake.
 
trigger LeadTrigger on Lead (Before Insert) {

    if(Trigger.isInsert && Trigger.isBefore){
        Set<String> setOfEmailIds = new Set<String>();
		list<Task> lstTask = new list<Task>();
		
        for(lead leadObj : Trigger.new){
            if(leadObj.email!= null && leadObj.leadSource =='Web'){ // look for leads whose source is web
                setOfEmailIds.add(leadObj.email);
            }
        }
		if(setOfEmailIds.size() > 0){
			Map<String,Contact> mapOFContact = new Map<String,Contact>(); // create map of email and contact
			for(Contact conObj : [Select id,email from contact where email in:setOfEmailIds]){
				mapOFContact.put(conObj.email , conObj);
			}
			
			for(lead leadObj : Trigger.new){
				if(mapOFContact.containsKey(leadObj.email)){ // show error if email Id found
					leadObj.addError('You are already registered with email Id with us');
				}
				else{ // create follow up task
					Task t = new Task();
					t.OwnerId = Add QueueId or User Id;
					t.Subject='Follow up for ' + leadObj.Name;
					t.Status='Not Started';
					t.Priority='Normal';
					lstTask.add(t);
				}
			}
			if(lstTask.size() > 0){
				insert lstTask;
			}
		}
    }
}

Hope this helps!
Dushyant SonwarDushyant Sonwar
Also you can check out this site to understand basics of trigger
https://www.sfdc99.com/beginner-tutorials/#beginnerTrigger