Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Context: I am trying to build a dupe counter that counts how many Leads match based on the Email value field. Here is the trigger:

//Look for duplicate contacts based on email
//Populate a Dupe_Lead_Counter__c field with a number value if >0 matches found
trigger CountDupeLeads on Lead (before insert, before update) {
    for (Lead l : Trigger.new) {
        //Make sure our lead has an email address
        if (l.Email != null) {
            //Find a dupe
            String leadEmail = l.Email;
            List<Lead> dupeLeads = [SELECT Id FROM Lead WHERE Email = :leadEmail];

            //If a dupe is found, update a field
            if (dupeLeads.size() > 0) {
                l.Dupe_Lead_Counter__c = dupeLeads.size();
            } else {
                //0 would designate a true unique
                l.Dupe_Lead_Counter__c = 0;
            }
        //want 'null' to distinguish Leads disqualified from the lookup versus true uniques
        } else {
            l.Dupe_Lead_Counter__c = null;
        }
    }

}

I then created a @isTest class:

@isTest
public class TestCountingDupes {
    static testMethod void testDupeCounter() {
        //Principle #ls: Create recordsd from Scratch           

        Lead dupeLead1 = new Lead();
        dupeLead1.FirstName = 'Peter';
        dupeLead1.LastName = 'Parker';
        dupeLead1.Email = 'spiderman@gmail.com';
        dupeLead1.Company = 'Tryler McTrevor Inc';
        insert dupeLead1;

        Lead dupeLead2 = new Lead();
        dupeLead2.FirstName = 'Jane';
        dupeLead2.LastName = 'Seymour';
        dupeLead2.Email = 'spiderman@gmail.com';
        dupeLead2.Company = 'Tryler McTrevor TEST Inc';
        insert dupeLead2;

        //Principle #2 use  System.assertEquals()
        dupeLead1 = [SELECT Id, Dupe_Lead_Counter__c FROM Lead LIMIT 1];
        System.assertEquals(2, dupeLead2.Dupe_Lead_Counter__c);

        //Principle #3: test things that shouldn't work!
        dupeLead2.Email = 'janeseymour@gmail.com';
        update dupeLead2;

        dupeLead1 = [SELECT Id, Dupe_Lead_Counter__c FROM Lead LIMIT 1];
        System.assertEquals(0, dupeLead2.Dupe_Lead_Counter__c);
    }
}

and the latest error I get from running the test is this:

System.AssertException: Assertion Failed: Expected: 2, Actual: null Class.TestCountingDupes.testDupeCounter

Clearly my trigger logic is populating a null value in that field before the Update action (when it should be the number 2), but I am at a loss--is it the way my @isTest is constructed with Lead record inserts and then updates or is it a systemic issue with my Apex trigger?

share|improve this question
up vote 2 down vote accepted

The reason for the failure is this part:

dupeLead1 = [SELECT Id, Dupe_Lead_Counter__c FROM Lead LIMIT 1];
System.assertEquals(2, dupeLead2.Dupe_Lead_Counter__c);

You are asserting that dupeLead2 has a certain value, but querying for dupeLead1. I would just get a fresh Lead and forget about reassigning previously declared variables. You can do:

Lead leadWithDupeEmail = [Select Id, Dupe_Lead_Counter__c from Lead where Id = :dupeLead2.Id limit 1];   
System.assertEquals(2, leadWithDupeEmail.Dupe_Lead_Counter__c); 

As a general comment, queries in for loops are bad. See https://developer.salesforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops

share|improve this answer
    
Thanks! I tried this, and got an error: 'System.AssertException: Assertion Failed: Expected: 2, Actual: 1 Class.TestCountingDupes.testDupeCounter: line 33, column 1' I may just change the System.Assert to equal that number since that is how it counts – GPP Mar 29 at 21:38

When you insert first lead, there won't be any dupes. So Dupe_Lead_Counter__c field in dupeLead1 will be 0.

When you insert the second lead, there is a dupe lead (only one as the trigger is on before) already present and trigger will update Dupe_Lead_Counter__c field in dupeLead2 as 1. It will never be 2.

Also query with specific lead id and check its Dupe_Lead_Counter__c field in assert statements like below.

    //Principle #2 use  System.assertEquals()
    dupeLead2 = [SELECT Id, Dupe_Lead_Counter__c FROM Lead where id =: dupeLead2.Id];
    System.assertEquals(1, dupeLead2.Dupe_Lead_Counter__c);

    //Principle #3: test things that shouldn't work!
    dupeLead2.Email = 'janeseymour@gmail.com';
    update dupeLead2;

    dupeLead2 = [SELECT Id, Dupe_Lead_Counter__c FROM Lead where id=: dupleLead2.Id];
    System.assertEquals(0, dupeLead2.Dupe_Lead_Counter__c);
share|improve this answer
    
So the question is when both Leads have email values inputted, but sequentially after the update, the System expects 'null' as opposed to '0'-- null should only occur if the Lead doesn't have an email value at all. – GPP Mar 29 at 21:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.