+ Start a Discussion
Amit Jadhav 13Amit Jadhav 13 

First exception on row 0; first error: REQUIRED_FIELD_MISSING, accountId must be specified if contactId is not null: [Id]

Test Class
@isTest
private class LeadConversionController_Test
{
     //Test Setup  
    @testSetup 
    static void setup() {
        Lead convertedLead = new Lead(LastName = 'Test Convert Lead No. 1',
                              Email='test@noreply.com');
        insert convertedLead;
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        Database.LeadConvert leadConvert = new Database.LeadConvert();
        leadConvert.setLeadId(convertedLead.Id);
        leadConvert.setDoNotCreateOpportunity(true);
        leadConvert.setOwnerId(UserInfo.getUserId());
        leadConvert.setConvertedStatus(convertStatus.MasterLabel);
        Database.convertLead(leadConvert);
        Lead lead = new Lead(LastName = 'Test Convert Lead No. 2',
                              Email='test@noreply.com');
        if(convertedLead.Email != lead.Email){
            insert lead;
        }
    }
    // Test Method: findDuplicates
    public static testmethod void Test_findDuplicates(){
        //Lead lead = [SELECT ID,Name,Email From Lead WHERE IsConverted = false];
        Lead lead = [SELECT ID,Name,Email From Lead];
        System.Assert(LeadConversionController.findDuplicates(lead) != null);
    }
    // Test Method: convertLeadRecord
    public static testmethod void Test_convertLeadRecord(){
        Account acc =TestFactory.getAccount();
        Contact con =TestFactory.getContact();
         Contact cont = new Contact(
         lastname='test',
         AccountId=acc.Id);
        insert cont;
        Lead lead =TestFactory.getLead();
        //Lead lead = [SELECT ID,Name,Email,OwnerId From Lead WHERE IsConverted = true];
        //Lead lead = [SELECT ID,Name,Email,OwnerId From Lead];
        //Alternate_Email__c alternateEmail = (Alternate_Email__c)System.JSON.deserialize(LeadConversionController.findDuplicates(lead).get('alternateEmail'), Alternate_Email__c.class);
        Alternate_Email__c alternateEmail= new Alternate_Email__c(
            Alternate_Email_Address__c = 'abc@gmail.com',
            Contact_Name__c = cont.Id,
            Active__c = true,
            Lead_name__c =lead.id
        );
        insert alternateEmail;
        string accountid = alternateEmail.Contact_Name__r.AccountId;
        string contactid = alternateEmail.Contact_Name__c;
        string leadid = lead.Id;
        string ownerId = lead.OwnerId;
        System.Assert(LeadConversionController.convertLeadRecord(accountid, contactid, leadid,ownerId) != '');
    }
    // Test Method: mergeLeadRecords
    public static testmethod void Test_mergeLeadRecords(){
        Lead masterlead = new Lead(LastName='Test Merge Lead #1',Email = 'test@noreply1.com');
        insert masterlead;
        Lead duplicateLeadNo1 = new Lead(LastName='Test Merge Lead #2',Email = 'test@noreply12.com');
        Lead duplicateLeadNo2 = new Lead(LastName='Test Merge Lead #3',Email = 'test@noreply13.com');
        List<Lead> duplicateLeads = new List<Lead>{duplicateLeadNo1,duplicateLeadNo2};
            insert duplicateLeads;
        string duplicateleadstring = JSON.serialize(duplicateLeads);
        System.Assert(LeadConversionController.mergeLeadRecords(masterlead, duplicateleadstring) != '');
    }
}
VinayVinay (Salesforce Developers) 
Hi Amit,

You need to set the required values to LeadConvert.  You have to set AccountId value.

>> convertedlead.setAccountId(accountid);  

Please review below link which helps you and provides you more information.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_convertlead.htm
https://salesforce.stackexchange.com/questions/271726/apex-class-throwing-error-that-accountid-is-mandatory
https://salesforce.stackexchange.com/questions/61359/account-required-field-missing-on-lead-conversion

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar