I am trying to create fields dynamically based on the CSV data. All CSV headers represent the field name in Salesforce and rows represent the values. As all CSV values are in string format I have to manually perform all checks and convert the CSV string value into appropriate Data Type using below function.

to get the appropriate value I am invoking the function as follows which returns the Object and later I can use to create dynamic field sobj.put('closeDate',getValue('DATETIME','2020-05-31 00:00:00'));

getValue('DATETIME','2020-05-31 00:00:00');

private Object getValue(String dataType, String value){

    if(dataType == 'STRING' || dataType == 'PHONE' || dataType == 'PICKLIST' || dataType == 'REFERENCE')
       return String.valueOf(value);
       if(dataType == 'DATE')
        return Date.valueOf(value);
        if(dataType =='BOOLEAN')
            return Boolean.valueOf(value);
        if(dataType =='CURRENCY')
            return  Decimal.valueOf(value);
            if(dataType == 'DATETIME')
                return Datetime.valueOf(value); //Datetime.valueOfGmt(value)

Now I am wondering do we have any other better approach to implement here as I really doubt how fisible is to convert String into Phone data type using String.valueOf. Please do suggest more dynamic and reliable approch.

  • What do you mean by 'create' fields dynamically? Commented May 24, 2020 at 9:48
  • For example, creating an pportunity field dynamically like sObject.put('Close Date', '20-05-2020) now here Stage field name and it's value is coming from CSV in a string format. But before creating field I need to determine the type of it
    – Hunt
    Commented May 24, 2020 at 10:22
  • sObject.put(fieldname, value) accepts generic Object type. You don't have to convert the data type, do you? Commented May 24, 2020 at 12:54
  • The value is in string as it's coming from CSV file. So you never know whether it is date or string or boolean
    – Hunt
    Commented May 24, 2020 at 12:54
  • Ah got it. posted an answer. Hopefully it works for you. Commented May 24, 2020 at 14:08

1 Answer 1


You should be able to get the data type from the DescribeFieldResult.getSOAPType() method. And then have a utility method to convert the String value to its corresponding field data type and return as a generic Object data type.

// get CSV Header and build the columns and their data types map
List<String> columns = new List<String>{'CloseDate','StageName','Description','Amount'};

Map<String, Schema.SObjectField> fieldMap = SObjectType.Opportunity.fields.getMap();
Map<String, SOAPType> columnTypeMap = new Map<String, SOAPType>();
for (String column : columns) {
    columnTypeMap.put(column, fieldMap.get(column.toLowerCase()).getDescribe().getSOAPType());

Object getFieldvalueAsObject(String fieldname, String stringValue) {
    if (columnTypeMap.get(fieldname) == SOAPType.String) {
        return stringValue;
    else if (columnTypeMap.get(fieldname) == SOAPType.Date) {
        return Date.valueOf(stringValue);
    else if (columnTypeMap.get(fieldname) == SOAPType.Double) {
        return Double.valueOf(stringValue);
    else {
        // other data type
        return null;

Opportunity opp = new Opportunity();
// the field value assignment here should be based on the CSV header instead of hardcoded
// just for example purposes
opp.put('CloseDate', getFieldvalueAsObject('CloseDate', '2020-06-30'));
opp.put('StageName', getFieldvalueAsObject('StageName', 'Prospecting'));
opp.put('Description', getFieldvalueAsObject('Description', 'My opportunity description'));
opp.put('Amount', getFieldvalueAsObject('Amount', '1000000'));
opp.put('Name', 'My oppt 2020');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .