1

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 mapping enter image description here

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);
2

This is expected, as noted in Considerations for Converting Leads:

When updating existing accounts or contacts during lead conversion, the values of mapped lead fields do not overwrite the values of the mapped account and contact fields.

| improve this answer | |

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.