+ Start a Discussion
AMalenaAMalena 

Bulkify the code

I'm still a fledgling Apex man right now.  I am trying to use a modified set of code from the following URL, but even the pre-existing code does not work llwhen deployed due to the error listed.

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_dedupe.htm

 

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.'); } }

 

Error:  Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, leadDuplicatePreventer: execution of BeforeInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact sales...

 

As much as I can write code and alter existing triggers, and as much as I keep reading this is "simply" a problem with not using an Indexed field, I have no clue what to do.  I've tried adding a Select to the MAP statement with a WHERE that references either Email or Name, but it doesn't help - same error.

 

Help?

AMalenaAMalena

The code being played with is even more simple:

 

trigger leadDuplicatePreventer on Lead(before insert) {

    Map<String, Lead> leadMap = new Map<String, Lead>();

    for (Lead lead : System.Trigger.new) {
        // 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);
        }
    }

    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.');
    }
}

 

hisalesforcehisalesforce

I dont see any problem.

1.May be some validation is creating a problem .Check it 

2.

[SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()] //put limit here and check it .....Like Limit 100
AMalenaAMalena
I just so happened to have tried a LIMIT 200 a few minutes ago - no go. I'm going through the Classes listed to see if I can find something there. :-/
AMalenaAMalena

I'm checking one of the key test classes.   The beginning of the class has:

 

    static testMethod void testLead1()            /* ----- Lead Testing Section ----- */
    {
//        CRMfusionDBR101.DB_Globals.triggersDisabled = true;

        Lead leadRec = new Lead();
        
        leadRec.FirstName = 'DSG';
        leadRec.LastName = 'DSGTestClassRecord Lead';
        leadRec.Phone = '4696751677';
        leadRec.Email = 'test@test.com';
        leadRec.LeadSource = 'FXCT DEMO';
        leadRec.Lead_Source_Detail__c = 'FXCT DEMO';
        leadRec.Status = 'New Lead';
        insert leadRec;
................

 

The error in question is mentioning the insert line of this code.  :-/