0

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='rsingh@da-desk.com';

    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.

  • what is the issue you are facing? – Ratan Paul Feb 16 '16 at 7:09
  • 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: [] – Russel baker 1 Feb 16 '16 at 7:18
1

When you are creating data that is wrong as per your trigger code.

You need to mentioned all the fields which used in your trigger.

I suggest add a check if this is null then don't create contact. Or change your test class code..

It should be like this.

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='r@na.com';
    lead.Second_Other_Contact_Name__c='Russell';
    lead.Second_Other_Contact_Phone__c='001234';
    lead.Second_Other_Contact_Email__c='r@na.com';
    lead.Third_Other_Contact_Name__c='Russell';
    lead.Third_Other_Contact_Phone__c='001234';
    lead.Third_Other_Contact_Email__c='r@na.com';

    insert lead;
  • 1
    Okay..Let me try – Russel baker 1 Feb 16 '16 at 7:25
  • @Russelbaker1 Yes I knew that I should work. – Ratan Paul Feb 16 '16 at 7:33

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.