+ Start a Discussion
Mike JackMike Jack 

Lead Conversion Field Mapping

Hi All,

Is there any possibility to get Lead Conversion Mapping Fields in Apex ??
I am trying to customize the Lead Conversion Process as per our requirement and I need to get what all Lead fields are mapped to Account/Contact/Opportunity. 
Please let me know if it is possible by anychance. 
Thanks in advance..
David HalesDavid Hales
Hi Balaji ,

I dont think you need to Write Apex code for this.
Follow below steps Setup Lead field mapping:
 
1. Go to Setup.
2. Under Build, click Customize | Leads | Fields.  
3. Under "Lead Custom Fields & Relationships," click the Map Lead Fields button.

You can map fields from here only.

Cheers :)

Please mark this answer as best answer if query is resolved so that it helps others
Mike JackMike Jack
Appreciate your response. But, my questions is different. 
What you said is 100% correct., what all fields we mapped there from Lead to Account/Contact/Opportunity, I need to get those field mappings in Apex.
Varun SinghVarun Singh
Hi @ balaji

Some time we need to pass one object information to another object. So in this regards developer needs to write a trigger / apex code and needs to define each column mapping in the code. But it may be possible that in future field mapping will change or some new fields will introduce. For that we again need to change code. 
So for above requirement we have created a object Pre-lead and on same object we have created a button convert lead. while converting the pre- lead into lead we need to provide dynamic field mapping between two object. For that i have created the custom setting "MyLeadToLeadMapping__c" with one custom field "Lead_Field_API_Name__c"
User-added image

Code:
 
public with sharing class ConvertMyLead
{
 public Boolean showmessage{get;set;}
 public string MyLeadid{get;set;}
 public Lead LeadObj;
 string qry = '';
    Map<string, string> MapMappingTable=new map<string,string>();

    public Pagereference MapLeadfields()
    {
        Pagereference pageref;
        Savepoint sp = Database.setSavepoint();
        try
        {
   LeadObj=new Lead();
   MyLeadid=ApexPages.currentPage().getParameters().get('MyLeadid');
   getAllMapping();
   qry = 'select ' + qry + 'id FROM My_Lead__c where id =: MyLeadid';
   My_Lead__c MyLead = Database.query(qry);

   for(String sMyLeadField: MapMappingTable.keySet())
   {
    String sLeadField = MapMappingTable.get(sMyLeadField);
    LeadObj.put(sLeadField, MyLead.get(sMyLeadField));
   }

   LeadObj.OwnerID = UserInfo.getUserId() ;
   LeadObj.status='new';
   insert LeadObj;
   showmessage=true;
   pageref=new Pagereference('/'+LeadObj.Id);
   return pageref;
        }
        catch(Exception ex)
        {
            Database.rollback(sp);
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            Apexpages.addMessage(msg);
            return null;
       }
    }
  
    public Map<string,string> getAllMapping()
    {
        qry ='';
        try{
             for (MyLeadToLeadMapping__c mappingTableRec : MyLeadToLeadMapping__c.getall().Values())
             {
                if (mappingTableRec.Name != null && mappingTableRec.Lead_Field_API_Name__c != Null )
                {
                    MapMappingTable.put(mappingTableRec.Name , mappingTableRec.Lead_Field_API_Name__c);
                    qry += mappingTableRec.Name + ',';
                }
             }
        }
        catch(exception ex)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            Apexpages.addMessage(msg);
        }
        return MapMappingTable;
    }
  
    public PageReference goBack()
    {
      PageReference pf = new PageReference('/'+MyLeadid);
      return pf;
    }

    public boolean getHasErrors()
    {
        return ApexPages.hasMessages(ApexPages.severity.ERROR);
    }
}

I Hope this is  useful information for you ,please mark my answer as best answer.

Thanks,
Varun

 
Mike JackMike Jack
Hi Varun Singh,
Really appreciate your response and this information with code might definitely useful for us.

We already have this workaround in mind to use Custom Settings or Custom Metadata Types. Camparing with your scenario with ours, your Pre-Lead is our Lead and your Lead is our Contact. When we have Standard Lead mapping fields., its like double work to again map fields in Custom Settings or Custom Metadata. 
We need to get the Mapping fields which are mapped from Lead to Contact in Apex as we are customizing conversion process.
Varun SinghVarun Singh
Hi @balaji


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