Dynamic field mapping in Salesforce : Amit Chaudhary

Dynamic field mapping in Salesforce
by: Amit Chaudhary
blow post content copied from  Apex Hours
click here to view original post

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. In this post we will learn about how to do Dynamic field Mapping in Salesforce using custom setting/Custom Metadata type.

What is Dynamic Mapping in Salesforce?

As a developer you may have faced this challenge to map the field and value while dynamically creating a record. If the field name for a source or target changes, you can configure a mapping to dynamically get metadata changes at runtime.

Take an example if you have 2 object and you want to insert the same object data in another object. But today you have 10 field on each object but in future it may increase. As per good design you should write a code in such a way if in future field will increase then you should not update the code. Code should do the field mapping dynamically.

How to develop Dynamic field mapping in Salesforce?

As per client requirement he want to store the Lead information in Pre-Lead and then he want to convert the pre-lead into Lead after some validation

In that case we need to create a dynamic field mapping between two object. That functionality  we can achieve with custom setting.


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”.

dynamic field mapping in Salesforce

After that add the field mapping in custom setting. Like below screen

Custom metadata type for dynamic field mapping


Now it time to field mapping dynamically from custom setting and create custom record.

public with sharing class ConvertMyLead {
    public Lead leadObj;
    String qry = '';
    public Lead createLead(String myLeadid) {
        leadObj = new Lead();
        try {
            Map<String, String> mapMappingTable = getAllMapping();
            qry = 'Select ' + qry + 'id FROM MyLead__c where id =: MyLeadid';
            MyLead__c MyLead = Database.query(qry);  
            String sLeadField='';
            for(String sMyLeadField: mapMappingTable.keySet()) {
                sLeadField = mapMappingTable.get(sMyLeadField);
                leadObj.put(sLeadField, MyLead.get(sMyLeadField));
            leadObj.OwnerID = UserInfo.getUserId() ;
            insert leadObj;
        } catch(Exception ex) {
            System.debug('Exception -->'+ex);
        return leadObj;
    public Map<string,string> getAllMapping() {
        qry ='';
        Map<String, String> mapMappingTable = new Map<String,String>();
        try {
             for (MyLeadToLeadMapping__mdt mappingTableRec : MyLeadToLeadMapping__mdt.getall().Values()) {
                if (mappingTableRec.DeveloperName != null && mappingTableRec.FieldAPIName__c != Null ) {
                    //mapMappingTable.put(mappingTableRec.DeveloperName , mappingTableRec.FieldAPIName__c);
                    mapMappingTable.put(mappingTableRec.FieldAPIName__c , mappingTableRec.DeveloperName);
                    qry += mappingTableRec.FieldAPIName__c + ',';
        } catch(exception ex) {
            System.debug('Exception -->'+ex);
        return mapMappingTable;


You can use custom setting or custom metadata/Custom Metadata type to store the dynamic field mapping in Salesforce. You can modify the custom field mapping anytime without changing the code.

The post Dynamic field mapping in Salesforce appeared first on Apex Hours.

August 15, 2022 at 03:02AM
Click here for more details...

The original post is available in Apex Hours by Amit Chaudhary
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.