I have a trigger on lead conversion with the help of my friend. please find
Test class failed every time. with error: System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadConvert: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName] Trigger.LeadConvert: line 17, column 1: []
below code:
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 )
{
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,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,AccountId = objLead.convertedAccountId));
lstContact.add(new contact(LastName = objLead.Third_Other_Contact_Name__c,Phone = objLead.Third_Other_Contact_Phone__c,
Email = objLead.Third_Other_Contact_Email__c,AccountId = objLead.convertedAccountId));
}
insert lstContact;
}
}
Then I wrote a test class but I am not able to figure put why this test class is fail.
@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='Rohit';
lead.First_Other_Contact_Phone__c='001234';
lead.First_Other_Contact_Email__c='[email protected]';
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();
}
}
Please help to figure out the issue.