1

I have the following test class LeadTriggerTest which fails with the following error:

System.AssertException: Assertion Failed: Expected: SQL, Actual: null Class.LeadTriggerTest.converted_opp_contacts_are_sql: line 31, column 1

Below is the code:

@isTest
private class LeadTriggerTest {

    // When a lead is converted to an opportunity, the Contact.Qualified__c 
    // for contacts related to the new opportunity should be set to SQL
    @isTest static void converted_opp_contacts_are_sql() {
        // Create lead
        Lead l = new Lead();
        l.LastName = 'TestLastName';
        l.Company = 'Test Company';
        insert l;

        // Make sure no opportunities in DB
        System.assertEquals(0, [ SELECT Count() FROM Opportunity ])<br>
        // Convert lead
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(l.Id);
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);

        // Make sure opportunities in DB and converted contact
        // has right value in Qualified__c

        Opportunity o = [ SELECT Id FROM Opportunity ];
        Contact c = [ SELECT Qualified__c FROM Contact
                      WHERE Id = :lcr.contactId ];

        system.debug(' contact value ' +   c.Qualified__c);

        System.assertEquals('SQL', c.Qualified__c);
    }
}

Apex Trigger:

trigger LeadTrigger on Lead (after update) {

    if (Trigger.IsUpdate && Trigger.IsAfter) {

        // Collect contact IDs from converted leads
        List<Id> contactIds = new List<Id>();
        for (Lead l : Trigger.new) {
            if (l.IsConverted && !Trigger.oldMap.get(l.Id).isConverted) {
                contactIds.add(l.ConvertedContactId);
            }
        }

        List<Contact> newContacts = [ SELECT Qualified__c 
                                      FROM Contact 
                                      WHERE Id IN :contactIds ];

        for (Contact newContact : newContacts) {
            newContact.Qualified__c = 'SQL';
        }

        update newContacts;
    }
}
  • 1
    Hi. Please provide your trigger code. Apparently, that's where the error comes from. – Martin Lezer Feb 19 '16 at 16:56
  • Could you add this line at the end of the test to make sure your lead field IsConverted is set to true? System.assertEquals(true, l.isConverted); – Martin Lezer Feb 19 '16 at 17:03
  • @MartinLezer Thankx , And do I need to remove the assert already written to make the test go fine – cloudy-ritz Feb 19 '16 at 17:06
  • Your current assert seems to be important. I just asked you to add the other assertion to check if your trigger is supposed to update the contacts list. – Martin Lezer Feb 19 '16 at 17:09
  • 4
    I'm voting to close this question as off-topic because it appears to be abandoned. – Adrian Larson Dec 3 '16 at 6:02
1

Is it possible that you need to also fetch the record Id alongside Qualified__c?

For example:

for (Contact newContact : [
    SELECT Id, Qualified__c 
    FROM Contact 
    WHERE Id IN :contactIds
]) {
    newContact.Qualified__c = 'SQL';
}

update newContacts;
  • 1
    Id is always returned in Apex queries, whether you specify it or not. – Adrian Larson Feb 19 '16 at 23:46
  • Thanks Adrian, was just a hunch. As for those: next time, test 'em first. – tobibeer Feb 20 '16 at 4:48

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