Converting lead to contact, when Email field of lead and contact is same.I queried lead and contact. Looped the Contact and lead checked the emails field, if it is same insert into a list. Then loop through that list and tried to convert lead into contact whose Email is same. But it is throuing an error at run time.
"System.DmlException: ConvertLead failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Converted objects can only be owned by users. If the lead is not owned by a user, you must specify a user for the Owner field.: [OwnerId]"
.
Queried code is:-
leadQuery_Result =[Select id, Email, Status, ownerID from Lead where Email != null and Status IN ('L3. Closed - Qualified New' , 'L3. Closed - Onboarding' , 'L3a. Closed - Existing') and
isConverted = False Limit 20 ];
Conversion code is:
List<Database.LeadConvert> lc_lst = new List<Database.LeadConvert>();
//Looping through lead List and adding Lead ID.
system.debug(' lead to convert :'+leadList_To_Convert);
System.Debug('Converting list Lead : ' + lc_lst);
for(Lead ltc : leadList_To_Convert){
System.Debug('Converted Lead: ' + ltc);
if(ltc.ID != null){
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(ltc.ID);
lc.setDoNotCreateOpportunity(True);
//lc.setOwnerId(ownerID);
lc.setConvertedStatus('L3a. Closed - Existing');
lc_lst.add(lc);
System.Debug('Converted Lead after conversion : ' + ltc);
}
}
System.Debug('Converting list Lead : ' + lc_lst);
// Lead Converting.
Database.LeadConvertResult[] lcr = Database.convertLead(lc_lst);
System.Debug('Converted Lead : ' + lcr);