0

I was looking for a way to retrieve Lead field mappings via Apex code and I didn't find anything related to this topic but I was wondering:

Is there any way to actually achieve this now?

Regards,

4
  • Hello, can you please elaborate, what do you mean by field mapping? Is it DescribeFieldResult for each Lead field? Or the type of data that can be mapped to a particular field? Nov 25 '21 at 10:40
  • @MariiaIllarionova Hi! I want to retrieve the field mapping from conversion. Just the field API names that map from Lead to Contact/Account/Opportunity
    – molinet
    Nov 25 '21 at 10:59
  • actually, it is possible to retrieve it using metadata api. Unfortunately, no native apex soultion for it as for now./ Nov 25 '21 at 13:14
  • @OleksandrBerehovskyi I tried using this: MetadataService.IReadResult readResult = service.readMetadata('LeadConvertSettings', new List<String>{'Lead'}) but I can't make it work. I tried without passing an array of fullNames too but still got nothing...
    – molinet
    Nov 25 '21 at 14:27
2

I have found a way, how to do it through Metadata API with Apex with help of this question and @Oleksandr Berehovskyi's answer for it.

It is possible to be retrieved through 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

Note, why your sample wasn't working, fullName for LeadConvertSettings is LeadConvertSettings itself.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.