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
frank2anilfrank2anil 

Error with LEad duplicate trigger

Hi

 

I want to check the email field and display the error message if the email already exists in my system.

i used the salesforce code but its giving me this error:-

Review all error messages below to correct your data.
Apex trigger Lead_Emailduplicate_preventonLead caused an unexpected exception, contact your administrator: Lead_Emailduplicate_preventonLead: execution of BeforeInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times):-

 

trigger leadDuplicatePreventer on Lead
                               (before insert, before update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new) {
        
        // Make sure we don't treat an email address that  
    
        // isn't changing during an update as a duplicate.  
    
        if ((lead.Email != null) &&
                (System.Trigger.isInsert ||
                (lead.Email !=
                    System.Trigger.oldMap.get(lead.Id).Email))) {
        
            // Make sure another new lead isn't also a duplicate  
    
            if (leadMap.containsKey(lead.Email)) {
                lead.Email.addError('Another new lead has the '
                                    + 'same email address.');
            } else {
                leadMap.put(lead.Email, lead);
            }
       }
    }
    
    // Using a single database query, find all the leads in  
    
    // the database that have the same email address as any  
    
    // of the leads being inserted or updated.  
    
    for (Lead lead : [SELECT Email FROM Lead
                      WHERE Email IN :leadMap.KeySet()]) {
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email '
                               + 'address already exists.');
    }
}

 

 

 

MoUsmanMoUsman

Hi frank2anil,

Please try this peice of apex code.

 

trigger leadDuplicatePreventer on Lead
                               (before insert, before update) {

//Created map for the lead
//@Key = email
//@Value = Lead   

Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new) {
            leadMap.put(lead.Email, lead);
    }

    for (Lead lead : System.Trigger.new) {
        
        // Make sure we don't treat an email address that  
    
        // isn't changing during an update as a duplicate.  
    
        if ((lead.Email != null) &&
                (System.Trigger.isInsert ||
                (lead.Email !=
                    System.Trigger.oldMap.get(lead.Id).Email))) {
        
            // Make sure another new lead isn't also a duplicate  
    
            if (leadMap.containsKey(lead.Email)) {
                lead.Email.addError('Another new lead has the same email address.');
            } 
       }
    }   
}

If it does not works please let me know. 

 --

Thanks

Usman

NiketNiket

Hi ,

 

You have to use a valid filter in this query. There are more than 100000 records eligible for your query. That is the reason you are getting the error. To resolve the error, the error message suggests itself :

 

1. You should use a filter against Null value 

2. should use a valid indexed filter.

 

Please let me know if it helps or not ?

 

Please mark it as the solution if it answers your question so that others can also take benifit. 

Niket Soral
Cyber Group Inc
Certified Administrator | Certified Developer
@anilbathula@@anilbathula@

Hi 

 

Strange this is not working and cant able to save the new records.

And i used filters for this to get solution but  could not able to get the solution.

Strange salesforce documentation code is not working.

 

 

 

Jeff MayJeff May

The catch here is that you have to use a filter on a 'selective' field.  When the Apex is being evaluated, if it is possible that your query will return more than 100,000 records, it won't actually be executed and you get the error.

 

So, even if you know your query will only return a single record, if it is "possible" that it returns more than 100,000, you need to use a different filter criteria and make it a 'selective' query.

 

Here is the doc that explains it in detail. Read the section on 'selective' queries:  http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm

 

 

Vinit_KumarVinit_Kumar

You can also contact support and get Email field custm indexed for you and that might resolve the issue.