Dynamic field mapping in Salesforce

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.

Solution

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

Code

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() ;
            leadObj.status='new';
            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;
    }
}

Summary

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.

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 461

3 Comments

  1. Great thought we have implemented the same for financial services client. Where all users related information will be stored on one object and that we need to populate it FSA object. Instead duplicateing the code we have created different different custom meta data for based on source and destination objects. Their we have maintained the mapping it’s great to recollect that use case again

  2. Great representation of dynamics , Thanks . However I was wandering how would it behave I we were to map value from a related field.

Leave a Reply

Your email address will not be published. Required fields are marked *