Skip to main content Join the Agentforce Virtual Hackathon to build innovative solutions and compete for a $50k Grand Prize. Sign up now. Terms apply.
Hi, 

I want to use custom metadata to make the mapping dynamic in salesforce. Can somebody please help how should i do it?

code snippet for mapping:

for(Staging_Course__c objB: [SELECT test_Field__c , test_field_1__c,name FROM Staging_Course__c WHERE Unique_Id__c IN: setUniqueId])

{

lstObjC.add(new Course__c( test_Field__c = objB.test_Field__c, test_field_1__c = objB.test_field_1__c , name = objB.name ));

I created a custom metadata record but i am not sure how to use to make the mapping dynamic in the above apex code.

custom metata in apex used for field mapping 

Thanks
6 answers
  1. Mar 29, 2018, 2:20 AM
    I am not clear on the object that you are trying to accomplish? Are you attempting to use Custom Metadata Types provided within Salesforce?

    Is it that you have data being sent to Salesforce from an external source (via a REST Integration or a middleware) and you need to provide the ability to map the incoming field's data with a corresponding field on an object wtihin Salesforce? Do all the incoming field values map to fields on the same object or do they map to various fields across multiple objects?

    Are you planning to include this feature as a part of the managed package?

    If you just intend to store a list of source and target field mappings you can use a List type of Custom Setting

    to store this information. Ideally you will need to have the following fields and you can build upon this.

    1. Source Object - Name of the data source. Could be either a standard custom object, a JSON key.
    2. Source Field - Specific field on the source if applicable
    3. Source Datatype - Datatype of the field for type conversion
    4. Target Object - API Name of the target Salesforce object
    5. Target Field - API name of the field on the target Salesforce object
    6. Target Datatype - Datatype of the field for type conversion

    Once you build this, you will need to leverage Dynamic DML from Dynamic Apex (

    https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_dml.htm

    )to create an Sobject instance at runtime based on the mapping and then put incoming data values to the appropriate target Sobject fields. Sample code is as below and you can build on it.

    public class DataMappingService{

      //Constrcutor

    public DataMappingService(){}

    public void insertDynamic(String SobjectTypeName){

    //Create the instance of the object based on the custom settings

    Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);

    if (targetType == null){

    //throw an exception

    }

    //Dynamic instantitation of Sobject

    Sobject sobjectInstance = targetType.newSObject();

      //Assign source data values to the dynamic Sobject instance fields

    for(String fieldName :<List of fieldnames from Custom Settings>){

    sobjectInstance.put(fieldName, <Your data value>)

    }

    //Insert the data

    Database.insert(sobjectInstance);

    }

    }

  2. Jul 14, 2020, 2:41 PM
    hi, there I have a similar situation. I am trying to query the custom metadata where when i select the picklist I need to return the values...this I  have so far

    public static List<CaseToDoListOptions__mdt> getCustumMetadata(String Type){

        string Obj ='CaseToDoListOptions__mdt';

        Schema.SObjectType objType   = Schema.getGlobalDescribe().get(Obj);

        

        Schema.DescribeSObjectResult r = objType.getDescribe();

        Map<String,Schema.SObjectField> fields = r.fields.getMap();

        

      

        List<CaseToDoListOptions__mdt> typeSelected = [SELECT Option_Value__c, Type__c FROM CaseToDoListOptions__mdt  WHERE Type__c =:Type];

    Can you help proceed ie: I am creating a map but i need only the list of values? how can I achieve that?
  3. Apr 4, 2018, 4:39 AM
    Hi,

    I was able to do it with custom metadata . Reason i used custom metadata was entity and field definition automatically populating just like dependent picklist values.

    Thanks

     
  4. Apr 1, 2018, 1:58 AM
    Hi,

    Not yet. I was thinking of using custom metadata instead of list custom settings . Not tried with list custom settings yet. Will try and uodate the post.
0/9000