I created a javascript button that will convert the current lead to a contact based on conditions that I've sent. It works as expected. The problem I have is the custom fields from the lead are not mapping to the contact after conversion.
The apex code the converts the lead
try{
// query a single contact that has the same first/lastName and email as the lead,
// and marketo sync is true.
// the contact will be the one that was last modified at the closest date to
// today.
Contact c = [SELECT Id, AccountId, OwnerId
FROM Contact
WHERE (Email = :l.Email
AND FirstName = :l.FirstName
AND LastName = :l.LastName
AND Marketo_Sync__c = true
AND Owner.Profile.Name IN :profileNames)
ORDER BY LastModifiedDate DESC
LIMIT 1
];
// if matched contact found, update the contact owner to be
// the same as the lead owner
c.OwnerId = l.OwnerId;
update c;
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(l.Id);
lc.setAccountId(c.AccountId);
lc.setContactId(c.Id);
lc.setDoNotCreateOpportunity(true);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
leadsToConvert.add(lc);
// throw an error if the contact soql fails
} catch (QueryException e) {
mergeErrors.add(e);
System.debug(e);
}
Database.LeadConvertResult[] lcrList = Database.convertLead(leadsToConvert, false);