I need control duplicate Lead in Web-To-Lead If Lead not exist, it must created, if not, the Lead will not create and a task created assigned to the existint Lead

It is possible? I want not create duplicate and matching rules, if not control this duplicate and create task if it's neccesary


1 Answer 1


You could use a trigger (after insert) to query existing records, if it finds a match, it then creates the task and deletes the incoming record. As a trivial example:

trigger FlagDuplicateLead on Lead (after insert) {
  Map<String, Id> leadsByEmail = new Map<String, Id>();
  for(Lead record: Trigger.new) {
    leadsByEmail.put(record.Email, null);
  for(Lead record: [SELECT Email FROM Lead WHERE Email = :leadsByEmail.keySet() AND Id != :Trigger.new]) {
    leadsByEmail.put(record.Email, record.Id);
  Lead[] leadsToDelete = new Lead[0];
  Task[] tasksToCreate = new Task[0];
  for(Lead record: Trigger.new) {
    Id dupLeadId = leadsByEmail.get(record.Email);
    if(dupLeadId != null) {
      tasksToCreate.add(new Task(WhoId=dupLeadId, Subject='Duplicate Lead', Status='Not_Started'));
  delete leadsToDelete;
  insert tasksToCreate;

This is just a rough draft; you may want to add error handling, other boundary checks, a trigger handler framework, etc.

As a side note, having duplicate leads is probably not a big deal, as this can help with campaign ROI and other stats. Instead, consider perhaps automating lead conversion so that you can maintain this historical data.

  • i think one would also have to check for intra-batch duplicate leads.
    – cropredy
    Commented Feb 3, 2021 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .