You need to sign in to do that
Don't have an account?
![k2018123 k2018123](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
custom metata in apex used for field mapping
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:
I created a custom metadata record but i am not sure how to use to make the mapping dynamic in the above apex code.
Thanks
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.
Thanks
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.
- Source Object - Name of the data source. Could be either a standard custom object, a JSON key.
- Source Field - Specific field on the source if applicable
- Source Datatype - Datatype of the field for type conversion
- Target Object - API Name of the target Salesforce object
- Target Field - API name of the field on the target Salesforce object
- 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.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.
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
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?