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.