3

I'd like to write a Lead trigger, to check for duplicate Contacts before inserting the lead, or converting it.

Contacts fields I'd like to check:

  • Firstname - should not be the same as the leads name

  • Lastname - should not be the same as the leads name

  • Email - should not be the same as the leads phone

After checking these fields & failing to find a duplicate, only then should the lead be converted.

3
  • I'm not really clear on what you're looking for. You want to disallow Lead conversion if there are any Contacts matching those 3 values?
    – Adrian Larson
    Commented Jul 26, 2013 at 15:02
  • There are free apps on the AppExchange for similar logic... Did I mention they're free?
    – sfdcfox
    Commented Jul 26, 2013 at 15:36
  • if i ll create lead , before that it check firstname , lastname , email does not exist in contact , if it exit shows some error msg , otherwise lead should be created
    – ganesh
    Commented Jul 29, 2013 at 5:51

2 Answers 2

13

Here's one possible scenario for your code. It doesn't catch misspellings, aliases, and other common data entry problems, but it is case insensitive.

trigger BlockDupeLeadConvert on Lead (after update) {
    class UniqueKey {
        UniqueKey(string Firstname, String lastName, String email) {
            this.firstname = firstname;
            this.lastname = lastname;
            this.email = email;
        }
        string FirstName, LastName, Email;
        integer hashCode() {
            return toString().hashCode();
        }
        boolean equals(Object o) {
            return toString() == ((UniqueKey)o).toString();
        }
        public override string toString() {
            return String.format(
                'FirstName = \'\'{0}\'\' AND LastName = \'\'{1}\'\' AND Email = \'\'{2}\'\'',
                new String[] {
                    FirstName==null?'':String.escapeSingleQuotes(FirstName.tolowercase()),
                    LastName==null?'':String.escapeSingleQuotes(LastName.tolowercase()),
                    Email==null?'':String.escapeSingleQuotes(Email.tolowercase())
                }
            );
        }
    }

    Map<UniqueKey, Lead> leads = new Map<UniqueKey, Lead>();
    for(Lead record:Trigger.new) {
        if(record.isconverted) {
            UniqueKey key = new UniqueKey(record.firstname, record.lastname, record.email);
            if(leads.containskey(key)) {
                record.addError('Duplicate lead conversion.');
            } else {
                leads.put(key, record);
            }
        }
    }
    String[] keys = new String[0];
    for(UniqueKey key:leads.keyset()) {
        keys.add(key.tostring());
    }
    String query = string.format(
        'SELECT Id,FirstName,LastName,Email FROM Contact WHERE {0}',
        new String[] {
            String.join(keys, ' OR ')
        }
    );
    if(!leads.isempty()) {
        for(Contact record:Database.query(query)) {
            UniqueKey key = new UniqueKey(record.firstname, record.lastname, record.email);
            if(leads.containskey(key)) {
                leads.get(key).addError('Duplicate lead conversion.');
            }
        }
    }
}
1
  • Note: Be careful with this code. It's carefully crafted to work around some "bugs" I found while writing this answer. I'm going to try and get those submitted.
    – sfdcfox
    Commented Jul 26, 2013 at 16:23
2

With new features, you don't even need any code. Use the duplicate matching rules in the declarative setup area to do this.

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