0

I have created a trigger on lead conversion to create contact of custom contact filed in lead. I have created some custom fields on lead object to capture alternate contact details apart from main contact.

First_Other_Contact_Name__c
First_Other_Contact_Phone__c
First_Other_Contact_Email__c
Second_Other_Contact_Name__c
Second_Other_Contact_Phone__c
Second_Other_Contact_Email__c

My requirement was when we normally convert lead so it become Account, opportunity and contact. Through below trigger custom contacts details became as second and third contact details under the same account.

trigger LeadConvert on Lead(after update) {
    map < Id, Lead > mapNewLead = trigger.newMap;
    List < Contact > lstContact = new List < Contact > ();
    for (Lead objLead: mapNewLead.values()) {
        If(objLead.isConverted == true && objLead.First_Other_Contact_Name__c != null && objLead.Second_Other_Contact_Name__c == null) {
            lstContact.add(new contact(LastName = objLead.First_Other_Contact_Name__c, Phone = objLead.First_Other_Contact_Phone__c,
                Email = objLead.First_Other_Contact_Email__c, Title = objLead.First_Other_Contact_Title__c, Level__c = objLead.First_Other_Contact_Level__c,
                Functional_Role__c = objLead.First_Other_Contact_Functional_Role__c, LinkedIn_URL__c = objLead.First_Other_contact_LinkedIn_URL__c,
                MobilePhone = objLead.First_Other_Contact_Mobile__c, AccountId = objLead.convertedAccountId));
        }
        if (objLead.isConverted == true && objLead.First_Other_Contact_Name__c != null && objLead.Second_Other_Contact_Name__c != null) {
            lstContact.add(new contact(LastName = objLead.First_Other_Contact_Name__c, Phone = objLead.First_Other_Contact_Phone__c,
                Email = objLead.First_Other_Contact_Email__c, Title = objLead.First_Other_Contact_Title__c, Level__c = objLead.First_Other_Contact_Level__c,
                Functional_Role__c = objLead.First_Other_Contact_Functional_Role__c, LinkedIn_URL__c = objLead.First_Other_contact_LinkedIn_URL__c,
                MobilePhone = objLead.First_Other_Contact_Mobile__c, AccountId = objLead.convertedAccountId));
            lstContact.add(new contact(LastName = objLead.Second_Other_Contact_Name__c, Phone = objLead.Second_Other_Contact_Phone__c,
                Email = objLead.Second_Other_Contact_Email__c, Title = objLead.Second_Other_Contact_Title__c, Level__c = objLead.Second_Other_Contact_Level__c,
                Functional_Role__c = objLead.Second_Other_Contact_Functional_Role__c, LinkedIn_URL__c = objLead.Second_Other_contact_LinkedIn_URL__c,
                MobilePhone = objLead.Second_Other_Contact_Mobile__c, AccountId = objLead.convertedAccountId));
        }
        insert lstContact;
    }
}

Code is working fine but when I wrote test class for this (see below) it gives me an error:

@isTest
public class TestLeadConvert {
    static testMethod void convertLead() {
        test.startTest();
        Lead lead = new Lead();
        lead.FirstName = 'Trigger1';
        lead.LastName = 'Test1';
        lead.Company = 'Trigger Test1';
        lead.LeadSource = 'Sales2.0';
        lead.First_Other_Contact_Name__c = 'Russell';
        lead.First_Other_Contact_Phone__c = '001234';
        lead.First_Other_Contact_Email__c = 'russell@dad.com';
        lead.First_Other_Contact_Title__c = 'Director';
        lead.First_Other_Contact_Level__c = 'Director';
        lead.First_Other_Contact_Functional_Role__c = 'Information tecnology';
        lead.First_Other_contact_LinkedIn_URL__c = 'www.linkedin.com';
        lead.First_Other_Contact_Mobile__c = '0098765';
        lead.Second_Other_Contact_Name__c = 'Mac';
        lead.Second_Other_Contact_Phone__c = '001234';
        lead.Second_Other_Contact_Email__c = 'mac@dad.com';
        lead.Second_Other_Contact_Title__c = 'Director';
        lead.Second_Other_Contact_Level__c = 'MD';
        lead.Second_Other_Contact_Functional_Role__c = 'Information tecnology';
        lead.Second_Other_contact_LinkedIn_URL__c = 'www.linkedin.com';
        lead.Second_Other_Contact_Mobile__c = '0098765';
        insert lead;
        System.debug('Created and inserted lead');
        //leadconvt.strAccountId= 'lead';
        //leadconvt.strContactId= 'lead';
        //leadconvt.convertlead();
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(lead.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 conversion was successful
        System.assert(lcr.isSuccess());
        test.stopTest();
    }
}

It's code coverage is 76% and it's not validating when I am trying to deploy it. Please help me to achieve 100% code coverage.

  • While validation and deployment, is there any error showing...? If any error showing, Please Update the Question with that ERROR. – Subhash May 24 '16 at 9:49
  • I have checked and update phone number according to validation. Now test class is passed without error but trigger code coverage is only 78% @Subhash – Russel baker 1 May 24 '16 at 9:52
3

There are a few issues with your trigger and your test code, and I think that you may be approaching the issue the wrong way.

You don't need 100% coverage to ensure the trigger work. What you need is to test (assert) the right behaviour.

Even if you managed to bring your code coverage to 100%, meaning that all lines are being evaluated and called, it doesn't mean that your trigger works, because you're not making the right asserts.

For instance, you assert: System.assert(lcr.isSuccess());. What this does is check that the lead conversion is successful, which is a feature that you didn't write: that's a Salesforce out-of-the box feature.

What you need to assert is that your "First other contact" and that your "Second other contact" are created successfully.

Something as simple as:

System.AssertEquals (3, [Select Id from Contact].size());

can assert that 2 additional contacts were created (the one for the lead conversion and your additional 2).

And if you test this with converting more than one lead at once you will expose some of the codes in your issue. For instance, at a glance, you're doing:

insert lstContact;

inside the for loop. If you were to convert 2 leads at once, you will find you have the wrong number of contacts created (ie: the extra contacts for the 1st lead will be inserted twice).

I wrote a post on how to test triggers. It describes a few cases you need to cover to write good unit tests. Writing those test cases will expose the issues in your code.

In short: Don't aim for 100% code coverage. Aim for testing the right cases.

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.