14

I've written some Apex code to override the lead conversion process. if the contact exists, the client wants to update all the contact fields with the lead field data when the lead field is not null.

There are over 40 field mappings. Is there a way to access the field mappings table so I don't have to hardcode the field names?

2

Now it is possible to retrieve Lead Conversion fields through Apex with help of Metadata API. To use this code you need to have MetadataService class from https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
service.CallOptions = new MetadataService.CallOptions_element();
service.timeout_x = 120000;

List<MetadataService.LeadConvertSettings> leadConvertSettings = (List<MetadataService.LeadConvertSettings>) service.readMetadata('LeadConvertSettings', new List<String>{'LeadConvertSettings'}).getRecords();

for (MetadataService.LeadConvertSettings leadConvertSetting : leadConvertSettings) {
    for (MetadataService.ObjectMapping objectMapping : leadConvertSetting.objectMapping) {
        System.debug(objectMapping.inputObject + ' -> ' + objectMapping.outputObject);
        for (MetadataService.ObjectMappingField fieldMapping : objectMapping.mappingFields) {
            System.debug('\t' + fieldMapping.inputField + ' -> ' + fieldMapping.outputField);
        }
    }
}
13

Unfortunately the Lead Conversion field mappings aren't available in APEX or the Metadata API

4
  • 2
    I suspected this myself, but I couldn't find the documentation that proves it. Do you have a link?
    – sfdcfox
    Oct 31 '13 at 19:08
  • 2
    Any time-saving solution or answer? Sep 19 '17 at 21:39
  • @SFDC_peter sorry, no Sep 21 '17 at 5:47
  • 1
    It is as for Winter '22 is available in metadata api Nov 25 at 13:17

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.