
Dynamically Assign values to fields of sObjects
During Lead Conversion Mapping, we need to map different data type fields with the same data type fields. Using some apex code customization, we can achieve this easily.
Here I am going to explain how we can dynamically populate fields of sObject can.
syntax- SobjectVariable.put(fieldApi,value);Account acnt = new Account();
acnt.put(‘Name’, New Account);
Insert acnt;
This helps to all requirements in which we have to dynamically assign a value to object’s fields, Fields
API
is stored as a string type variable.
Example 1-In this example, I am trying to explain how can we get all Fields API name, their field type and Dynamically assign value based on their field type.
Account acc = new Account();
//Instance of Account
//Get All fields API of Account Object
String Sobjectname = ‘Account’;
Map<String, Schema.SObjectField> fields = Schema.getGlobalDescribe().get(sobjectname).getDescribe().SObjectType.getDescribe().fields.getMap();
for(Schema.SObjectField field : fields.values())
{
Schema.DescribeFieldResult fieldResult = field.getDescribe();
// check whether field is isUpdateable
if(fieldResult.isUpdateable())
{
string fieldName= field.getDescribe().getName();
Schema.DisplayType fielddataType = fields.get(fieldName).getDescribe().getType();
string FieldType = string.valueOf(fielddataType);
if(fieldName ==’Name’)
//if field API is Name
{
acc.put(fieldName,’Test Account’);
}
if(FieldType ==’INTEGER’)
acc.put(fieldName,1000);
//It puts 1000 in all Fields which of INTEGER types
if(FieldType ==’CURRENCY’)
acc.put(fieldName,5000);
//It puts 1000 in all Fields which of Currency types
if(FieldType ==’TEXTAREA’)
acc.put(fieldName,’This is text Area field’);
//It Populates all fields of TEXTAREA type
}
}
insert acc;
Created record of Account-

In this example, I am trying to create a record of Account object from an already created record of Contact. I have a field to field Mapping stored in a custom Label(or can use custom setting or
Custom Metadata Types
).

Now I have to assign values to Account field from contact field as stored in custom label.
public class DynamicPopulate {
public void method(){
Account acnt = new Account();
//Instance of Account
String someLabel = System.Label.Mapping;
//Label in which Mapping is stored
// map to store Account field as key and contact fields as values
Map<string,string>MapAcountToContact = new Map<string,string>();
//Call function which return a Map
MapAcountToContact = splitString(someLabel);
//Id of Contact record
ID recordId = ‘
XXXXXXXX
‘;
DescribeSObjectResult describeResult = recordId .getSObjectType().getDescribe();
Map<String, Schema.SObjectField> Contactfeilds=describeResult.fields.getMap();
List<String> ContactfieldNames = new List<String>( Contactfeilds.keySet());
//Dynamic Query which On Contact which fetch all columns at a time
String query = ‘ SELECT ‘ + String.join( ContactfieldNames , ‘,’ ) + ‘ FROM ‘ + describeResult.getName() + ‘ WHERE ‘ + ‘ id = :recordId ‘ + ‘ LIMIT 1 ‘;
SObject records = Database.query( query );
Contact objOfCon = (Contact) records;
String Sobjectname = ‘Account’;
//Map to store All Fields of Account
Map<String, Schema.SObjectField> fields = Schema.getGlobalDescribe().get(sobjectname).getDescribe().SObjectType.getDescribe().fields.getMap();
for(Schema.SObjectField field : fields.values())
{
Schema.DescribeFieldResult fieldResult = field.getDescribe();
// check whether field is isUpdateable
if(fieldResult.isUpdateable())
{
string NameOfField = field.getDescribe().getName();
//Get field type of Account’s fields
Schema.DisplayType fielddataType = fields.get(NameOfField).getDescribe().getType();
string AccountfieldType = string.valueOf(fielddataType);
//Check if Account field is in custom label
if(MapAcountToContact.containsKey(NameOfField))
{
String s2 =MapAcountToContact.get(NameOfField);
//Get field type of Contact’s fields
Schema.DisplayType fielddataType2 = Contactfeilds.get(s2).getDescribe().getType();
string ContactfieldType = string.valueOf(fielddataType);
if(AccountfieldType ==ContactfieldType )
{
acnt.put(NameOfField,objOfCon.get(s2));
}
}
}
}
insert acnt;
}
//Method which split mapping stored in custom label in string format and stores in a Map, //Account’s fields are stored as key and Contact’s fields are stored as value
public map<string,string> splitString(string str)
{
Map<String,string> MapAfterSplit = new Map<String,string>();
if(str != null && str != ”)
{
str=str.trim();
List<String> lst = str.split(‘;’);
for(string s : lst)
{
s= s.trim();
List<String> lst2 = s.split(‘-‘);
MapAfterSplit.put(lst2[0],lst2[1]);
}
}
return MapAfterSplit;
}
}
Reference-
https://developer.salesforce.com/docs/atlas.en.us.apexcode.meta/apexcode/apex_methods_system_sobject.htm