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
Surender reddy SalukutiSurender reddy Salukuti 

Request1

Hi Every one,

How can i write program checking Duplicate fields in Lead object FirstName,Email,company,phone using trigger concept and also display message if it is Dublicate its not dublicate record will save.
SANDEEP CHITTINENISANDEEP CHITTINENI
  • Write SOQL on the lead object and take four set<String> for each field you want as shown in below code.
    set<String> nameSet = new Set<String>();
    set<String> emailSet = new Set<String>();
    set<String> companySet = new Set<String>();
    set<String> phoneSet = new Set<String>();
    for(Lead l : [select firstname, email, company, phone from lead]){
    nameSet.add(l.firstname);
    emailSet.add(l.email);
    companySet.add(l.company);
    phoneSet.add(l.phone);
    }
    
    for(Lead lead : Trigger.new){
    if(nameSet.contains(lead.firstname)){
    lead.addError('First name is duplicate');
    }
    else if(emailSet.contains(lead.email)){
    lead.addError('Email is duplicate');
    }
    else if(companySet.contains(lead.company)){
    lead.addError('Company is duplicate');
    }
    else if(phoneSet.contains(lead.phone)){
    lead.addError('Phone is duplicate');
    }
    else{
    }
    }
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi,

I would suggest another approach to solve this problem. Suppose you have a large number of leads (50,000 or more), the query [select name, email, company, lead phone] will fail.

I think the best approach is to filter the query with the fields of the new records. For example:
trigger LeadTrigger on Lead (before insert) {
    
    if( Trigger.isBefore && Trigger.isInsert ) {
        Set<String> firstNamesSet = new Set<String>(); 
        Set<String> lastNamesSet = new Set<String>(); 
        Set<String> companiesSet = new Set<String>(); 
        Set<String> phonesSet = new Set<String>(); 

        for( Lead l : Trigger.new ) {
            firstNamesSet.add( l.FirstName );
            lastNamesSet.add( l.LastName );
            companiesSet.add( l.Company );
            phonesSet.add( l.Phone );
        }

        // Use this query to match any field
        // List<Lead> existingLeads = [Select Id, FirstName, LastName, Company, Phone from Lead where FirstName in :firstNamesSet OR LastName in :lastNamesSet OR Company in :companiesSet OR Phone in :phonesSet];

        // Use this query to match all fields
        List<Lead> existingLeads = [Select Id, FirstName, LastName, Company, Phone from Lead where FirstName in :firstNamesSet and LastName in :lastNamesSet and Company in :companiesSet and Phone in :phonesSet];

        for( Lead l : Trigger.new ) {
            for( Lead existingLead : existingLeads ) {
                Boolean doBrake = false;
                if( l.FirstName == existingLead.FirstName && l.LastName == existingLead.LastName && l.Company == existingLead.Company && l.Phone == existingLead.Phone ) {
                    if( l.FirstName == existingLead.FirstName ) {
                        l.addError('First name is duplicate');
                        break;
                    }

                    if( l.LastName == existingLead.LastName ) {
                        l.addError('Last name is duplicate');
                        break;
                    }

                    if( l.Company == existingLead.Company ) {
                        l.addError('First name is duplicate');
                        break;
                    }

                    if( l.Phone == existingLead.Phone ) {
                        l.addError('Phone name is duplicate');
                        break;
                    }
                }
            }
        }

    }
}

One other question: have you ever consider to use matching and duplicate rules on Leads?

Hope you find this solution useful.

Best regards.