1

I have this beforeUpdate trigger on Lead, which is calls a static method to populate 3 different fields based on data passed to a Picklist field.

Field definitions:

  • Campus__c: Picklist
  • Secondary_Campus__c: Lookup to a custom object named Campus
  • Tertiary_Campus__c: Lookup to a custom object named Campus
  • Quaternary_Campus__c: Lookup to a custom object named Campus

How the logic is supposed to work is:

  1. Web-forms / Marketo sends in a 3 letter campus code via the Campus__c picklist field.
  2. This method checks whether old and new values of Campus__c field are same or not and if not same, step 3.
  3. Check the fields secondary_campus__c, tertiary_campus__c etc. in order to see whichever is empty/null and populate it with the matching Campus ID from the Campus object.

Note: There is also a field called Primary_campus__c which gets populated in a similar manner during the beforeInsert cycle

Trigger: Linked method

public static void addMultipleCampuses( List<Lead> newLeads, List<Lead> oldLeads ) {

    map<String,Campus__c> mapCampusData = CommLib.getCampusMap();

// Update
if( TriggerMonitor.runOnce() ) {        

    // Checks for changes in lead data
    for( Integer i = 0; i < newLeads.size(); i++ ) {

        // If Campus__c doesn't match
        if( oldLeads[i].Campus__c != NULL && 
            newLeads[i].Campus__c != NULL &&
            newLeads[i].Campus__c != oldLeads[i].Campus__c ) {

            if( newLeads[i].Secondary_Campus__c == NULL && 
                newLeads[i].Tertiary_Campus__c == NULL &&
                newLeads[i].Quaternary_Campus__c == NULL ) { 
                // Fill Secondary Campus
                newLeads[i].Secondary_Campus__c = mapCampusData.get( newLeads[i].Campus__c ).Id;
                continue;
            } // sec campus == null?

            if( newLeads[i].Secondary_Campus__c != NULL && 
                newLeads[i].Tertiary_Campus__c == NULL &&
                newLeads[i].Quaternary_Campus__c == NULL ) {
                // Fill Tertiary Campus
                newLeads[i].Tertiary_Campus__c = mapCampusData.get( newLeads[i].Campus__c ).Id;
                continue;
            } // ter campus == null?

            if( newLeads[i].Secondary_Campus__c != NULL && 
                newLeads[i].Tertiary_Campus__c != NULL &&
                newLeads[i].Quaternary_Campus__c == NULL ) {
                // Fill Quaternary Campus
                newLeads[i].Quaternary_Campus__c = mapCampusData.get( newLeads[i].Campus__c ).Id;
            } // quat campus == null?

        } // if

        i++;

    } // for

    } // TriggerMonitor.runOnce()

}

Test Method

static testMethod void testMultipleCampusAssignments() {  

    // Start Test
    test.startTest();

try {

    Lead leadA = new Lead();
    leadA.FirstName = 'Multi Campus';
    leadA.LastName = 'Test Ting';
    leadA.Email = 'test@gmail.com';
    leadA.Phone = '1234567890';
    leadA.Country = 'United Kingdom';
    leadA.Status = 'New';
    leadA.LeadSource = 'Direct Mail';

    // Specify Campus
    leadA.Campus__c = 'CHI';

    // =============
    System.debug( '==== Inserting Lead with CHI ====' );
    insert leadA;

    ID leadId = leadA.ID;

    // First Test - Primary Campus
    leadA = [SELECT Id, Campus__c, Primary_Campus__r.Shortcode__c FROM Lead WHERE Id = :leadId];
    System.assertEquals( leadA.Campus__c, leadA.Primary_Campus__r.Shortcode__c );

    // Update Campus
    leadA.Campus__c = 'SFO';
    update leadA;
    leadA = [SELECT Id, Campus__c, Secondary_Campus__r.Shortcode__c FROM Lead WHERE Id = :leadId];
    System.assertEquals( 'SFO', leadA.Secondary_Campus__r.Shortcode__c );

    // Update Campus
    leadA.Campus__c = 'NASH';
    update leadA;
    leadA = [SELECT Id, Campus__c, Tertiary_Campus__r.Shortcode__c FROM Lead WHERE Id = :leadId];
    System.assertEquals( 'NASH', leadA.Tertiary_Campus__r.Shortcode__c );

    // Update Campus
    leadA.Campus__c = 'MIA';         
    update leadA;
    leadA = [SELECT Id, Campus__c, Quaternary_Campus__r.Shortcode__c FROM Lead WHERE Id = :leadId];
    System.assertEquals( 'MIA', leadA.Quaternary_Campus__r.Shortcode__c );            

}
catch( Exception e ) {
    System.debug( e.getMessage() );
}         

    // Stop Test
    test.stopTest();

}

No matter whatever I try, the test fails at the 2nd System.assert() onwards and highlights the 3 conditional blocks in the method as not covered. enter image description here

Can anyone please point out why the test keeps failing?

Thanks.

9
  • Have you tried adding debugging to see what values are in Campus__c , Secondary_Campus__c, Tertiary_Campus__c and Quaternary_Campus__c? You may also want to check the value of i as well in your for loop – BarCotter May 28 '15 at 13:22
  • Check the values of your campus lookup fields after insert. It looks like those fields may get populated on insert. If so, then none of your inner if statements would result with true. – dBeltowski May 28 '15 at 13:25
  • Printing out the error in the catch block of your test is poor practice. If your trigger throws an error your test would still pass. – BarCotter May 28 '15 at 13:33
  • 1
    Never put your whole test in a try catch block! It cannot fail and is therefore useless and very difficult to understand the results. – Adrian Larson May 28 '15 at 15:00
  • 1
    In a test method like you have, there should be no need for a try-catch at all. In fact, your test.startTest shouldn't occur until after or just before you insert your lead. Before if your trigger fires after insert. After if it doesn't fire until after update. – crmprogdev May 28 '15 at 16:06
1

This was too much to put in a comment. In your test code here, where your problems begin:

// Update Campus
    leadA.Campus__c = 'SFO';
    update leadA;
    leadA = [SELECT Id, Campus__c, Secondary_Campus__r.Shortcode__c FROM Lead WHERE Id = :leadId];
    System.assertEquals( 'SFO', leadA.Secondary_Campus__r.Shortcode__c );

It would seem that your query and asserts should look like:

leadA = [SELECT Id, Campus__c, Primary_Campus__r.Shortcode__c, Secondary_Campus__r.Shortcode__c FROM Lead WHERE Id = :leadId];
System.assertEquals( 'SFO', leadA.Primary_Campus__r.Shortcode__c);
System.assertEquals( 'CHI', leadA.Secondary_Campus__r.Shortcode__c);

With the update, you've presumably changed the primary campus to 'SFO' and the previous primary campus is now the secondary campus.

Edit in response to comments:

Your code says:

newLeads[i].Secondary_Campus__c = mapCampusData.get( newLeads[i].Campus__c ).Id;

I'm not certain what mapCampusData is, but it doesn't appear to be assigning the old value of Campus__c to Secondary_Campus__c for the lead as something like the below would, assuming oldLeadsmap is the equivalent of trigger.Oldmap.

newLeads[i].Secondary_Campus__c = oldLeadsmap.get(newLeads[i].Id).Campus__c

You'd then still need another statement which assigns the value to your custom object. The above only assigns the value to your custom field on the lead. Unless one is a look-up to the other, you'd need to do both. Perhaps that's what your mapCampusData does? If so, perhaps your statement above isn't correct? Perhaps you don't need the .ID at the end of the statement?

2
  • Hi, perhaps my explanation wasn't clear enough. Here's how the flow happens: 1. On INSERT, the value in Campus__c get's translated to the ID of the Campus relational object and Primary_campus__c is populated with it. 2. On every subsequent UPDATES, 2nd, 3rd and 4th campus fields get populated in a similar manner, as long as they're blank. thanks. – miCRoSCoPiC_eaRthLinG May 28 '15 at 15:34
  • First, @adrianlarson is correct, you don't need a try-catch block for any of your test. See my edited response above. – crmprogdev May 28 '15 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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