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:

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();
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        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) {

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

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

        update newContacts;

closed as off-topic by Adrian Larson, Ratan Paul, crmprogdev, Mohith Shrivastava, Lance Shi Dec 5 '16 at 1:03

  • This question does not appear to be about Salesforce within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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

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.