[SalesForce] Test class showing error : Converted Account empty for a Converted Lead.: [ConvertedAccountId]

I need to write as test class to cover

public without sharing class CMassConvert {
    public boolean hasErrors;
    public String errorMsg {get; set;}
    //constructora
    public CMassConvert() {}

    public List<Database.LeadConvertResult> MassConvert(List<Lead> listOfLeads) {
        if(listOfLeads != null && listOfLeads.size() > 0) {
            try {
                Map<Id, Account> mapOfAccounts = new Map<Id, Account>();
                List<Id> listOfContactIds = new List<Id>();
                List<Lead> updatedLeadsCB = new List<Lead>();
                Id UsrId;

                //Get Converted Lead status
                String convertedLeadStatusValue = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;

                if(convertedLeadStatusValue == null || convertedLeadStatusValue == '')
                    convertedLeadStatusValue ='Closed - Converted';

                Set<String> setOfFistName = new Set<String>();
                Set<String> setOfLastName = new Set<String>();
                Set<String> setOfAddress = new Set<String>();
                Set<String> setOfCity = new Set<String>();
                Set<String> setOfState = new Set<String>();
                Set<String> setOfZip = new Set<String>();

                for(Lead led : listOfLeads) {
                    setOfFistName.add(led.FirstName);
                    setOfLastName.add(led.LastName);  
                    setOfAddress.add(led.Street);
                    setOfCity.add(led.City);
                    setOfState.add(led.State);
                    setOfZip.add(led.PostalCode); 

                }

                // Check for any duplicate contacts
                List<Contact> listOfContact = [SELECT Id, FirstName, LastName, MailingStreet,  
                                               MailingCity, MailingState, MailingPostalCode, AccountID 
                                               FROM Contact 
                                               WHERE FirstName IN :setOfFistName AND LastName IN :setOfLastName
                                               AND MailingStreet IN :setOfAddress AND MailingCity IN :setOfCity
                                               AND MailingState IN :setOfState AND MailingPostalCode IN :setOfZip];

                Map<String, Contact> mapOfSearchContacts = new Map<String, Contact>();

                for(Contact c : listOfContact) {
                    String key = '';

                    if(c.FirstName != null)
                        key += c.FirstName.toLowerCase();
                    if(c.LastName != null)
                        key += c.LastName.toLowerCase();
                    if(c.MailingStreet != null)
                        key += c.MailingStreet.toLowerCase();
                    if(c.MailingCity != null)
                        key += c.MailingCity.toLowerCase();
                    if(c.MailingState != null)
                        key += c.MailingState.toLowerCase();
                    if(c.MailingPostalCode != null)
                        key += c.MailingPostalCode.toLowerCase();    
                    if(mapOfSearchContacts.ContainsKey(key) == false) {
                        mapOfSearchContacts.Put(key, c);
                    }
                }

                // Map<Id, Contact> mapOfContacts = new Map<Id, Contact>([SELECT Id, AccountID FROM Contact WHERE Id IN :listOfContactIds]);
                List<Database.LeadConvert> listOfLeadConvert = new List<Database.LeadConvert>();

                for(Lead led : listOfLeads) {
                    Database.LeadConvert lc = new database.LeadConvert();

                    if(ConvertedLeadStatusValue != null && ConvertedLeadStatusValue != '') {
                        lc.setConvertedStatus(convertedLeadStatusValue);
                    }

                    lc.setLeadId(led.id);

                    String leadKey = '';

                    if(led.FirstName != null)
                        leadKey += led.FirstName.toLowerCase();
                    if(led.LastName != null)
                        leadKey += led.LastName.toLowerCase();
                    if(led.Street != null)
                        leadKey += led.Street.toLowerCase();
                    if(led.City != null)
                        leadKey += led.City.toLowerCase();
                    if(led.State != null)
                        leadKey += led.State.toLowerCase();
                    if(led.PostalCode != null)
                        leadKey += led.PostalCode.toLowerCase();

                    Contact c = mapOfSearchContacts.get(leadKey);

                    if(c != null && c.AccountID != null) {
                        lc.SetContactID(c.Id);
                        lc.SetAccountID(c.AccountID);
                    }

                    // updatedLeadsCB.add(led);
                    // Get current user id to fill into converted by field in Leads based on custom setting else use default user in Lead
                    Map<String, CMLeadSetting__c> mapOfCMLeadSetting = CMLeadSetting__c.getAll(); 
                    if(mapOfCMLeadSetting.containsKey('CMLead Conversion')) {
                        if( mapOfCMLeadSetting.get('CMLead Conversion').Logged_in_User_For_Conversion__c ) {
                            UsrId = UserInfo.getUserId();
                        } else {
                            UsrId = led.OwnerId;
                        }                                                
                    }
                    lc.setOwnerId(UsrId);
                    lc.setOpportunityName(led.company + '-' + String.valueof(Date.today()));
                    listOfLeadConvert.add(lc);
                }

                System.debug('Converting Leads: ' + listOfLeadConvert); 
                List<Database.LeadConvertResult> lcr = Database.convertLead(listOfLeadConvert);

                for (Database.LeadConvertResult sLCR : lcr) {
                    System.debug('Opportunity Id for ' + sLCR.getLeadId() + ' = ' + sLCR.getOpportunityId());
                }
                return lcr;
            } catch(Exception e) {
                system.debug(e.getMessage());
                //throw e;
                this.hasErrors = true;
                this.errorMsg = e.getMessage();
                return null;
            }              
        } else {
            return null;
        }                
    }    
}

For this I wrote my test class as

Account a = new Account (); 
a.Name = 'Account name';
insert a;

Lead l = new Lead();
l.Company = 'LeadCompany';
l.IsUnreadByOwner__c = true;
l.Description = 'leadDescription';
l.Status = 'Open - Not Contacted';
l.FirstName = 'LeadFirstname';
l.LastName ='Leadlastname';
l.Street = 'Closed - Converted';
l.City = 'LeadCity';
l.State = 'LeadState';
l.PostalCode = '09877';
l.IsConverted = true;
l.Account__c = a.id;

insert l;

/*
    id usrprofileId=userinfo.getProfileId();
    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(l.id);
    lc.setDoNotCreateOpportunity(true);
    lc.setOwnerId(usrprofileId);
    lc.setConvertedStatus('Qualified');

    Database.LeadConvertResult lcr = Database.convertLead(lc);
*/

list<Lead> l_lst = new list<Lead>();
l_lst = [SELECT IsUnreadByOwner__c from lead where id = : l.id];

PageReference pageRef = Page.massConvert;
Test.setCurrentPage(pageRef); 
pageRef.getParameters().put('id', l.id); 

CMassConvert cm = new CMassConvert();
cm.MassConvert(l_lst);

I am getting an error when I run the test class

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Converted Account empty for a Converted Lead.: [ConvertedAccountId]

I know I am missing out some thing but cannot identify what is the issue properly.

Best Answer

This error happened because the Converted Lead in your source tool doesn't have the following fields populated:

1.Converted Account ID 2.Converted Contact ID 3.Converted Opportunity ID. These fields are mandatory in Salesforce, therefore this error is returned.

Related Topic